最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Deleting particular data from a json object using JavaScript - Stack Overflow

programmeradmin2浏览0评论

I am using titanium for developing Android application. I want to delete some data from json object. My json object given below:

{"feeds":
[
    {"username":"abc","user":"abc","feed":{"description":"dss","id":660,"user_id":1}},
    {"username":"bcd","user":"bcd","feed":{"description":"dddd","id":659,"user_id":1}}
]
}

for receiving json object I used following code

var json = this.responseText;
var json = JSON.parse(json);
json.feeds.splice(0,1);
alert(json.feeds[0]);

I want to delete particular data from json object like json.feeds[0] using JavaScript. I am able to access json.feeds[0] but not able to delete. Is there any way to delete that data from json object using JavaScript?

I am using titanium for developing Android application. I want to delete some data from json object. My json object given below:

{"feeds":
[
    {"username":"abc","user":"abc","feed":{"description":"dss","id":660,"user_id":1}},
    {"username":"bcd","user":"bcd","feed":{"description":"dddd","id":659,"user_id":1}}
]
}

for receiving json object I used following code

var json = this.responseText;
var json = JSON.parse(json);
json.feeds.splice(0,1);
alert(json.feeds[0]);

I want to delete particular data from json object like json.feeds[0] using JavaScript. I am able to access json.feeds[0] but not able to delete. Is there any way to delete that data from json object using JavaScript?

Share Improve this question edited Aug 15, 2015 at 12:07 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 8, 2011 at 8:48 nilkashnilkash 7,54633 gold badges103 silver badges183 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You are using splice to properly remove an element from a javascript array:

json.feeds.splice(0,1)

Using the code you've provided it will look like:

(function(){
    var json = {
        "feeds": [
            {"username":"abc","user":"abc","feed":{"description":"dss","id":660,"user_id":1}},
            {"username":"bcd","user":"bcd","feed":{"description":"dddd","id":659,"user_id":1}}
        ]
    };

    json.feeds.splice(0,1);
    console.log(json.feeds); // just to check that "feeds" contains only a single element
})();
  1. Parse the JSON into a JavaScript data structure
  2. Use splice to remove the elements you don't want from the array
  3. Serialise the JavaScript objects back into JSON
发布评论

评论列表(0)

  1. 暂无评论