json 数组的索引值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4231327/
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
Index value of Array
提问by ashu
I have a json file with contents like this:
我有一个内容如下的json文件:
{
"aaa":{
"status":"available",
"classkey":"dotnet"
},
"bbb":{
"ccc":{
"com":"available",
"net":"available",
"info":"available",
"org":"available"
}
}
}
Now I want to fetch the value of array by index (ex., xxx[0]like this not like xxx['aaa']). How do I do that?
现在我想通过索引获取数组的值(例如,xxx[0]像这样而不是像xxx['aaa'])。我怎么做?
回答by Alex
You can't. Order is not guaranteed in json and most other key-value data structures. Furthermore you don't have an array, you have an object. Why use name-value pairs at all if you aren't going to use the namesto access the values? Therein lies the power of json and other key-value data stores. If you needto access the values by integer indexes, then just use an array to store your data.
你不能。在 json 和大多数其他键值数据结构中不保证顺序。此外,您没有数组,您有一个对象。如果您不打算使用名称来访问值,为什么要使用名称-值对呢?这就是 json 和其他键值数据存储的强大之处。如果您需要通过整数索引访问值,那么只需使用数组来存储您的数据。
回答by Xdevelop
Actually you can use intindex like an array in JSON, just try that:
实际上你可以int像 JSON 中的数组一样使用索引,试试看:
var jsonObject = {
"0":{
"status":"available",
"classkey":"dotnet"
},
"1":{
"ccc":{
"com":"available",
"net":"available",
"info":"available",
"org":"available"
}
}
}
alert(jsonObject[0].status)
回答by Ignacio Vazquez-Abrams
You don't have an array, you have an object. As such you cannot expect the keys to be in the same order on all systems. Iterate the object as objects are expected to be iterated, that is, over the keys you are given.
你没有数组,你有一个对象。因此,您不能期望所有系统上的密钥都具有相同的顺序。迭代对象,因为对象应该被迭代,也就是说,在你给定的键上。
回答by Chase
Use the for...in construct, and then use array syntax. Here is an example.
使用 for...in 构造,然后使用数组语法。这是一个例子。
for (var key in xxx) {
document.write(xxx[key]);
}
回答by Barbarrosa
Never used JSON, but according to http://labs.adobe.com/technologies/spry/samples/data_region/JSONDataSetSample.html#Example1, it's possible to create an ordered array. Just remove the key and colon in your key-value pairs. (Someone please tell me whether this works - it may be dependent on values being defined in all objects, or it may allow null columns).
从未使用过 JSON,但根据http://labs.adobe.com/technologies/spry/samples/data_region/JSONDataSetSample.html#Example1,可以创建有序数组。只需删除键值对中的键和冒号。(有人请告诉我这是否有效 - 它可能取决于在所有对象中定义的值,或者它可能允许空列)。
回答by Diego Mendes
Maybe this can solve your problem.
也许这可以解决您的问题。
Using jQuery you can convert your JSon to Array and access it by index.
使用 jQuery,您可以将 JSon 转换为 Array 并通过索引访问它。
var data = $.parseJSON(msg.d ? msg.d : msg);
alert(data[1].status)
回答by texnic
For the sake of providing an answer to the question title, here is the array structure that would serve the original purpose:
为了提供问题标题的答案,这里是用于原始目的的数组结构:
[
{
"status":"available",
"classkey":"dotnet"
},
{
"ccc":{
"com":"available",
"net":"available",
"info":"available",
"org":"available"
}
}
]
So just replace the outmost brackets with [] to get an array instead of an object. Here is a demo. See http://www.json.org/and http://www.json.com/.
所以只需用 [] 替换最外面的括号来获得一个数组而不是一个对象。这是一个演示。请参阅http://www.json.org/和http://www.json.com/。

