另一个数组值中的数组值

时间:2013-08-01 19:13:01

标签: javascript jquery json

我想将数组值嵌套在另一个数组中。我不确定为什么这不起作用:

$.getJSON("test.js", function(json){

  varservers=["test02","test07","test08","test01","test05"];

  for(vari=0;i<servers.length;i++){

    var mine=[json.server[0].servers[i][0].version,json.server[0].servers[i][0].SerialNumber,json.server[0].servers[i][0].Manufacturer,json.server[0].servers[i][0].FreePhysicalMemory];

$("div").append("<div>Version: "+mine[0]+"</div><div>Serial Number: "+mine[1]+"</div><div>Manufacturer: "+mine[2]+"</div><div>FreePhysicalMemory: "+mine[3]+"</div>");
  };
});

这是保存为js的json格式。如果我不将服务器作为数组,它就可以工作:

{
"server": [
    {
        "test02": [
            {
                "version": "5.2.3790",
                "SerialNumber": "69712-640-24775",
                "Manufacturer": "Microsoft Corporation",
                "FreePhysicalMemory": "182128"
            }
        ]
    },
    {
        "test07": [
            {
                "version": "5.2.3790",
                "SerialNumber": "69713-650-26411",
                "Manufacturer": "Microsoft Corporation",
                "FreePhysicalMemory": "4101680"
            }
        ]
    },
    {
        "test08": [
            {
                "version": "5.2.3790",
                "SerialNumber": "69712-650-4015",
                "Manufacturer": "Microsoft Corporation",
                "FreePhysicalMemory": "2578328"
            }
        ]
    },
    {
        "test05": [
            {
                "version": "6.1.7601",
                "SerialNumber": "00477-001-004673",
                "Manufacturer": "Microsoft Corporation",
                "FreePhysicalMemory": "744060"
            }
        ]
    },
    {
        "test04": [
            {
                "version": "6.1.7601",
                "SerialNumber": "00477-001-04534",
                "Manufacturer": "Microsoft Corporation",
                "FreePhysicalMemory": "14263600"
            }
        ]
    }
]
}

*这篇文章是由另一个用户编辑的,以使json看起来更具可编辑性,但是这一更改使jquery编码变得冗长且难以阅读。我将该代码更改回以前的状态。

1 个答案:

答案 0 :(得分:1)

以下是遇到同一问题的任何人的工作代码。密钥对存储在一个单独的.js文件中。

$(document).ready(function(){
srv=[aus01,aus02,aus03,aus04,aus05];
for ( i=0;i<srv.length;i++ ) {
 var y =[srv[i]][0];
  for ( x in y ) {
$("#lists").append("<div id='" + y[x].Name + "'> \
                   Server Name:" + y[x].Name + "<br> \
                   Serial Number:" + y[x].SerialNumber + "<br> \
                   Version:" + y[x].version + "<br> \
                   Manufacturer:" + y[x].Manufacturer + "<br> \
                   Free Physical Memory:" + y[x].FreePhysicalMemory + "<br><br> \
                   </div>");
  };
};
});