-1

I want to nest an array value inside another array. I'm uncertain why this won't work:

$.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>");
  };
});

Here is the json format saved as js. It works if I don't make the servers an array:

{
"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"
            }
        ]
    }
]
}

*This post was edited by another user to make the json look more editable, but the change made the jquery coding long and hard to read. I changed that code back to its previous state.

4

1 回答 1

1

Here is the working code for anyone that comes across the same issue. The key pairs are stored in an array in a seperate .js file.

$(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>");
  };
};
});
于 2013-08-02T16:46:29.960 回答