在 Javascript 中解析二维 JSON 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2488148/
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
Parse 2 dimensional JSON array in Javascript
提问by MrG
I have a two dimensional JSON array where each element contains several attributes. The example below is intentionally simplified:
我有一个二维 JSON 数组,其中每个元素都包含多个属性。下面的例子是有意简化的:
var map_data = { "1":
{"1":{"name":"aa"},"2":{"name":"bb"}},
"2":
{"1":{"name":"cc"},"2":{"name":"dd"}}
};
I try to parse the data but .lengthdoesn't work:
我尝试解析数据但.length不起作用:
for(x=1; x<=map_data.length; x++) {
for(y=1; y<=map_data[x].length; y++) {
// CODE
}
}
Many, many thanks!
非常感谢!
回答by CMS
That's not an array, they are simple objects, that's why you cannot use the lengthproperty.
那不是数组,它们是简单的对象,这就是您不能使用该length属性的原因。
You need to use the for...instatement:
您需要使用以下for...in语句:
for(var x in map_data) {
if (map_data.hasOwnProperty(x))
for(var y in map_data[x]) {
if (map_data[x].hasOwnProperty(y)) {
// CODE
}
}
}
The hasOwnPropertychecks are because this statement iterates over all properties, inherited or not, and if something (like some JavaScript frameworks) augmented the Array.prototypeor Object.prototypeobjects, those augmented properties will be also iterated.
该hasOwnProperty检查是因为该语句将遍历所有属性,继承或没有,如果事情(像一些JavaScript框架)增强的Array.prototype或Object.prototype对象,这些增强的特性也将被重复。
You should know that this statement doesn't ensure anyhow the order of iteration.
您应该知道该语句无论如何都不能确保迭代顺序。
I would recommend you to use a "real" array:
我建议您使用“真实”数组:
[
[{"name":"aa"},{"name":"bb"}],
[{"name":"cc"},{"name":"dd"}]
]
In this way you will be able to use the lengthproperty to iterate over the indexes.
通过这种方式,您将能够使用该length属性来遍历索引。
回答by kennytm
Since map_datais an object instead of an array you need to use for/inloop:
由于map_data是对象而不是数组,因此您需要使用for/in循环:
for (var x in map_data) {
// check for hasOwnProperty omitted for simplicity.
for (var y in map_data[x]) {
// CODE.
}
}
But it's better to send that JSON as an array [a,b,c]instead of an object {"0":a,"1":b,"2":c}.
但最好将该 JSON 作为数组[a,b,c]而不是 object 发送{"0":a,"1":b,"2":c}。
回答by Li0liQ
hasOwnPropertyis used to determine whether an object has the specified property as a direct property of that object with no respect to it's prototype chain.
hasOwnProperty用于确定对象是否具有指定的属性作为该对象的直接属性,而不考虑它的原型链。
for(var i in map_data){
if(map_data.hasOwnProperty(i)){
for(var j in map_data[i]){
if(map_data[i].hasOwnProperty(j)){
/*do whatever you want with map_data[i][j]*/
}
}
}
}
回答by Eli Bendersky
Use the for .. inconstruct:
使用for .. in构造:
for (var m in map_data) {
// ...
}
Also, I must note that this isn't a "JSON array", but rather a nested object. JSON is just the string representation of nested JS objects and arrays.
另外,我必须注意这不是“JSON 数组”,而是嵌套对象。JSON 只是嵌套 JS 对象和数组的字符串表示。
回答by user1738256
if it helps someone, its a example of c# and javascript:
如果它对某人有帮助,它就是一个 c# 和 javascript 的例子:
c#:
C#:
List<List<string>> list_array = new List<List<string>>();
JavaScriptSerializer jss = new JavaScriptSerializer();
string _myJSONstring = jss.Serialize(list_array);
/*in this case list_array is a list bidimensional, but can be a single array, if you print _myJSONstring, this will show like this: "[["XX","AAAA"],["YY","BBBB"]]" */
/* 在这种情况下,list_array 是一个二维列表,但可以是单个数组,如果你打印 _myJSONstring,它将显示如下: "[["XX","AAAA"],["YY","BBBB"] ]" */
javascript:
javascript:
into a function that get the string from c#:
进入一个从 c# 获取字符串的函数:
var a = JSON.parse(array);
for (var t = 0; t < array.length; t++) {
for (v = 0; v < array[t].length; v++) {
alert(array[t][v]);
}
}

