[英]JSON can't access data returns undefined
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. 我希望包含“ Fashion Forward”的
json.name
但是我得到undefined
返回值,但是如果我console.log(json)
我可以看到它是一个对象。
Where am I going wrong? 我要去哪里错了?
json
是数组的名称,您可以像这样使用json[0].name;
Why you use Array
? 为什么使用
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
.我希望包含“ Fashion Forward”的
json.name
但是我得到未定义的返回值,但是如果我console.log(json)
我可以看到它是一个object
。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
现在您可以使用
json.name
As it looks from your code, json is an array of single item. 从代码中可以看出 , json是单个项目的数组。 Try this
尝试这个
json[0].name
Let me know if it works for you. 请让我知道这对你有没有用。
the json variable is an array . json变量是一个数组 。 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 要获取第一个对象,您需要像
json[0]
一样选择它,然后您可以像这样访问name属性
var name = json[0].name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.