English 中文(简体)
Java文中的双层JSON阵列
原标题:Parse 2 dimensional JSON array in Javascript

我有两阵列,每个单元都有几个属性。 以下实例是有意简化的:

var map_data = { "1":
                      {"1":{"name":"aa"},"2":{"name":"bb"}},
                 "2":
                      {"1":{"name":"cc"},"2":{"name":"dd"}}
               };

我试图对数据进行分类,但<编码>。 工作:

for(x=1; x<=map_data.length; x++) { 
    for(y=1; y<=map_data[x].length; y++) {
        // CODE 
    }
}

很多人表示感谢!

最佳回答

这并不是一个阵列,而是简单的物体,因此,你无法使用<条码><<>length财产。

您需要使用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
      }
    }
}

www.un.org/Depts/DGACM/index_french.htm 检查的原因在于,这一声明渗透了所有继承或未继承的财产,如果某种(如一些 Java本框架)补充了<条码>。

你们应当知道,这一说法确实确保了任何 it。

我建议你使用“再”阵列:

[
 [{"name":"aa"},{"name":"bb"}],
 [{"name":"cc"},{"name":"dd"}]
]

这样,你就能够使用<代码>length 的财产,以在索引上进行检索。

问题回答

<代码>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]*/
            }
        }
    }
}

由于<代码>>>>_data是一个物体,而不是需要使用<条码>的阵容。 住宿:

for (var x in map_data) {
  // check for hasOwnProperty omitted for simplicity.
  for (var y in map_data[x]) {
    // CODE.
  }
}

但最好把JSON作为阵列发送[a,b,c],而不是一个物体{0”:a,"1:b,”2:c}

使用<代码>。 构造:

for (var m in map_data) {
     // ...
}

此外,我必须指出,这只是一个“JSON阵列”,而是一个nes物体。 JSON只是 JS缩地代表了联合材料的目标和阵列。

如果它帮助某人,它就是一个 c和 j印的例子:

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"]]" */

javascript:

a) 从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]);
        }
    }




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签