I have the following JSON:
{
"list": {
"q": "raw",
"sr": "28",
"ds": "Standard Reference",
"start": 0,
"end": 1,
"total": 1391,
"group": "",
"sort": "n",
"item": [
{
"offset": 0,
"group": "Poultry Products",
"name": "Chicken, broilers or fryers, dark meat, thigh, meat only, raw",
"ndbno": "05096",
"ds": "SR"
}
]
}
}
But when I try to access elements in this json i keep getting undefined error... I already checked if the JSON is valid and yes the json is valid! Maybe I am accesing the JSON wrong? The JSON is stored in a variable called result. This is how I tried so far:
console.log(result.list.ds);
I also tried:
console.log(result["0"].list.ds);
but both solutions give me the following error:
Uncaught TypeError: Cannot read property 'ds' of undefined
Why is this happening? Any tips or suggestions are welcome!
Is your JSON stored in your variable as a string? Try:
console.log(JSON.parse(result).list.ds)
The reason I ask is that the text Uncaught TypeError: Cannot read property 'ds' of undefined
tells me that your result
variable does not have a list
property, which your JSON clearly would if it were an object, but it most assuredly would not if you had accidentally forgotten to parse your JSON string.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments