Javascript 像数组一样访问 json 对象中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14408281/
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
Access elements in json object like an array
提问by holyredbeard
Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?
I have a json object, like the one below:
我有一个 json 对象,如下所示:
[
["Blankaholm", "Gamleby"],
["2012-10-23", "2012-10-22"],
["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har.],
["57.586174","16.521841"], ["57.893162","16.406090"]
]
It consists of 4 "property levels" (city, date, description and coordinates).
它由 4 个“属性级别”(城市、日期、描述和坐标)组成。
What I want to do is to be able to access these levels like in an array like this:
我想要做的是能够像这样的数组一样访问这些级别:
var coordinates = jsonObject[4];
This does obvious not work so my question is how can I do it?
这显然不起作用,所以我的问题是我该怎么做?
Do I need to decode it or something, and if so how?
我需要解码它还是什么,如果需要,如何解码?
回答by holyredbeard
I found a straight forward way of solving this, with the use of JSON.parse.
我找到了解决这个问题的直接方法,使用 JSON.parse。
Let's assume the json below is inside the variable jsontext.
让我们假设下面的 json 在变量jsontext 中。
[
["Blankaholm", "Gamleby"],
["2012-10-23", "2012-10-22"],
["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har.],
["57.586174","16.521841"], ["57.893162","16.406090"]
]
The solution is this:
解决办法是这样的:
var parsedData = JSON.parse(jsontext);
Now I can access the elements the following way:
现在我可以通过以下方式访问元素:
var cities = parsedData[0];
回答by Ragnarokkr
The your seems a multi-array, not a JSON object.
你似乎是一个多数组,而不是一个 JSON 对象。
If you want access the object like an array, you have to use some sort of key/value, such as:
如果要像数组一样访问对象,则必须使用某种键/值,例如:
var JSONObject = {
"city": ["Blankaholm, "Gamleby"],
"date": ["2012-10-23", "2012-10-22"],
"description": ["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har.],
"lat": ["57.586174","16.521841"],
"long": ["57.893162","16.406090"]
}
and access it with:
并通过以下方式访问它:
JSONObject.city[0] // => Blankaholm
JSONObject.date[1] // => 2012-10-22
and so on...
or
或者
JSONObject['city'][0] // => Blankaholm
JSONObject['date'][1] // => 2012-10-22
and so on...
or, in last resort, if you don't want change your structure, you can do something like that:
或者,作为最后的手段,如果你不想改变你的结构,你可以这样做:
var JSONObject = {
"data": [
["Blankaholm, "Gamleby"],
["2012-10-23", "2012-10-22"],
["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har.],
["57.586174","16.521841"],
["57.893162","16.406090"]
]
}
JSONObject.data[0][1] // => Gambleby
回答by Jeffrey Sweeney
I noticed a couple of syntax errors, but other than that, it should work fine:
我注意到几个语法错误,但除此之外,它应该可以正常工作:
var arr = [
["Blankaholm", "Gamleby"],
["2012-10-23", "2012-10-22"],
["Blankaholm. Under natten har det varit inbrott", "E22 i med Gamleby. Singelolycka. En bilist har."], //<- syntax error here
["57.586174","16.521841"], ["57.893162","16.406090"]
];
console.log(arr[4]); //["57.893162","16.406090"]
console.log(arr[4][0]); //57.893162
回答by Paolo
var coordinates = [jsonObject[3][0],
jsonObject[3][0],
jsonObject[4][1],
jsonObject[4][1]];

