Sunday, 13 November 2016

javascript - loop through a json object in ajax response





I am new to json so i am getting a json reponse from my ajax call



now i am stuck with looping the json object



here is my json



 {
"0": {
"image": "http://test.com/systems.jpg",
"anchor_tag_link": "http://test.com/1",

"title": "Oct-Dec-2013"
},
"1": {
"image": "http://test.com/energy.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "July-Sept-2013"
},
"pages": 2
}



Can anyone help


Answer



You can use a for-in loop as follows:



var obj = {
"0": {
"image": "http://test.com/systems.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "Oct-Dec-2013"

},
"1": {
"image": "http://test.com/energy.jpg",
"anchor_tag_link": "http://test.com/1",
"title": "July-Sept-2013"
},
"pages": 2
}

for(var prop in obj) {

var item = obj[prop];
console.log(item);
}


Be aware that you will get the items in your log because you will get the pages property in addition to the numeric properties.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...