在JSON对象访问元素,比如一个数组 [英] Access elements in json object like an array
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-i-access-a-specific-value\">I有一个嵌套的数据结构/ JSON,我怎么可以访问特定的值?
我有一个JSON对象,像下面这样:
I have a json object, like the one below:
[
["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"]
]
它由4属性级别(城市,日期,描述和坐标)。
It consists of 4 "property levels" (city, date, description and coordinates).
我想要做的是能够访问这些水平就像一个数组是这样的:
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?
我是否需要取消code,或什么的,如果又如何?
Do I need to decode it or something, and if so how?
推荐答案
我找到解决这个,配合使用JSON.parse的一个直接的方式。
I found a straight forward way of solving this, with the use of JSON.parse.
假设下面的JSON是变量中的 jsontext
Let's assume the json below is inside the variable 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];
这篇关于在JSON对象访问元素,比如一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!