开发者

Deleting particular data from a 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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜