I’m trying to get a JSON response and iterate through the data. Here is the response I get:
[{"category":{"category":"sell"},"subcategory":{"subcategory":"cars"}}]
Then I run that through JSON.parse() and then when I try to access data I get undefined.
for (var category in myJson) {
console.log(category.category); //this is undefined
}
Does anyone have any ideas on why this isn’t working as I am intending to? Is my JSON format incorrect?
Try this:
Your code is wrong because you’re trying to access the ‘category’ property of the iterator variable.