I know this may seem basic to some of you, but I am trying to iterate a json object. I apologize as I seem to be having difficulty getting various examples to work. I’ve also read the jQuery documentation and the json examples listed there don’t quite match my json structure.
Here’s a link to my example which is displaying “null” in the console where I expected to see “1”. I’m just trying to print out the article_id element of each “node” in the json array.
var json = [
[{
"article_id": 1,
"article_title": "test",
"article_content": "test1"
}, {
"article_id": 2,
"article_title": "test2",
"article_content": "this is a second test article"
}]
];
$.each(json, function (arrayID, group) {
console.log(group.article_id);
});
Your json is not an array of object, it is an array of array of object.
When you enter in your iteration, for each element, the arrayID is the index (0, 1, etc…) and the group is the sub array.
To solve your problem, use json[0] instead of json in your code