引用 JSON (Javascript) 对象的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2074908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
refer to an element of JSON (Javascript) object
提问by mart
How do I refer to an element of JSON (Javascript) object. exampe: alert(homes.Agents[1].name);
如何引用 JSON (Javascript) 对象的元素。例如:警报(homes.Agents[1].name);
<script>
var homes = [
{
"Agents" : {
"name" : "Bob Barker",
"name" : "Mona Mayflower"
},
"Listings" : [
{
"h_id": "3",
"city": "Dallas",
"state": "TX",
"zip": "75201",
"price": "162500"
},
{
"h_id": "4",
"city": "Bevery Hills",
"state": "CA",
"zip": "90210",
"price": "319250"
},
{
"h_id": "5",
"city": "New York",
"state": "NY",
"zip": "00010",
"price": "962500"
}
]
}
];
</script>
回答by Crast
That's not exactly very good JSON above there, in the case of the Agents value the second key will override the first.
上面的 JSON 并不是很好,在 Agents 值的情况下,第二个键将覆盖第一个。
You probably meant:
你可能的意思是:
"Agents" : [
{"name" : "Bob Barker"},
{"name" : "Mona Mayflower"}
],
Then you'd access the first agent's name as
然后你可以访问第一个代理的名字
homes[0]['Agents'][0]['Name']
Similarly, to get one of the values from the Listings, you'd do something akin to:
同样,要从 Listings 中获取其中一个值,您需要执行以下操作:
homes[0]['Listings'][0]['city']
- or -
homes[0].Listings[0].city
The dot syntax can be used wherever there is a valid identifier, else you need to use the array syntax.
只要有有效标识符,就可以使用点语法,否则您需要使用数组语法。
As a side note, I'm not sure the structure of the data, but it's possible that you can eliminate the outer-level []that's enclosing your whole structure in an array. Then you wouldn't need to access everything as homes[0]['Listings']and instead simply homes['Listings'].
作为旁注,我不确定数据的结构,但是您可以消除将[]整个结构包含在数组中的外部级别。那么你就不需要访问所有东西homes[0]['Listings'],而只是简单地homes['Listings'].
回答by Jordan Running
Your JSON syntax is wrong. You can't have the same key twice in an object. Instead, you need an array:
您的 JSON 语法错误。一个对象中不能有两次相同的键。相反,您需要一个数组:
var homes = {
"Agents" : [
{ "name" : "Bob Barker" },
{ "name" : "Mona Mayflower" }
],
...
}
Then you can access the agents like so:
然后你可以像这样访问代理:
homes.Agents[1] // => { "name": "Mona Mayflower" }
// or
homes.Agents[1].name // => "Mona Mayflower"
回答by sberry
Homes is an Array, so your first accessor is index based.
Homes 是一个数组,所以你的第一个访问器是基于索引的。
homes[0]
家[0]
Agents is an Object, and Object containing two keys of the same name. That is a no-no.
Agents 是一个 Object,而 Object 包含两个同名的键。这是一个禁忌。
If you are defining this data yourself, you should change
如果你自己定义这些数据,你应该改变
"Agents": {
"name" : "Bob Barker",
"name" : "Mona Mayflower"
}
to
到
"Agents": [
{"name" : "Bob Barker"},
{"name" : "Mona Mayflower"}
]
Then you could access the data in question by
然后您可以通过以下方式访问相关数据
homes[0].Agents[1].name
回答by Derek Swingley
You should give your object properties unique names. In your example, the second name property is overwriting the first so that homes[0].Agents.name will always be "Mona Mayflower".
你应该给你的对象属性唯一的名字。在您的示例中,第二个 name 属性将覆盖第一个,因此 home[0].Agents.name 将始终为“Mona Mayflower”。
Once you get that sorted, I think you're looking for this:
一旦你得到排序,我想你正在寻找这个:
homes[0].Agents.name
回答by gtd
This is invalid JSON Agents is just an object, not an array. If it was restructured like this:
这是无效的 JSON 代理只是一个对象,而不是一个数组。如果像这样重组:
"Agents" : [
{ "name" : "Bob Barker"},
{ "name" : "Mona Mayflower"}]
Then you could use homes.Agents[1].nameto get 'Mona Mayflower'
然后你可以用homes.Agents[1].name得到'莫娜五月花'
回答by Brett Widmeier
Example taken from the following link:
取自以下链接的示例:
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
"myJSONObject.bindings[0].method" will return "newURI"
“ myJSONObject.bindings[0].method”会返回“ newURI”

