Reputation: 6976
I have the following code,
var json = [
{
"name": "Fashion Forward",
"good": {
"doors" : {
"name1" : "ff_good_doors_1.jpg",
"name2" : "ff_good_doors_2.jpg",
"name3" : "ff_good_doors_3.jpg"
}
},
"better": {
},
"best": {
}
}
]
I would expect to be able get data out by doing something like,
json.name
which I would expect to contain "Fashion Forward" - however I get undefined
return, but if I console.log(json)
I can see that it is an object.
Where am I going wrong?
Upvotes: 0
Views: 104
Reputation: 2636
Why you use Array
? if you want to access members like what you already said :
I would expect to be able get data out by doing something like,
json.name
which I would expect to contain "Fashion Forward" - however I get undefined return, but if Iconsole.log(json)
I can see that it is anobject
.Where am I going wrong?
use this code and remove array:
var json = {
"name": "Fashion Forward",
"good": {
"doors" : {
"name1" : "ff_good_doors_1.jpg",
"name2" : "ff_good_doors_2.jpg",
"name3" : "ff_good_doors_3.jpg"
}
},
"better": {},
"best": {}
}
Now you may use json.name
Upvotes: 1
Reputation: 3937
the json variable is an array. to get the first object you need to select it like this json[0]
and then you can access the the name property like this
var name = json[0].name;
Upvotes: 0
Reputation: 21842
As it looks from your code, json is an array of single item. Try this
json[0].name
Let me know if it works for you.
Upvotes: 0