0

I have this Json:

{
"version": "firstVersion",
"numberID": "3",
"totalIDs": [
    {
        "1": {
            "subTot": "2",
            "subTotID": {
                "1": "aaa",
                "2": "bbb"
            }
        },
        "2": {
            "subTot": "2",
            "subTotID": {
                "1": "www",
                "2": "rrr"
            }
        }
    }
  ]
}

I know how to get the value of version, numberID, but when I try to drill down to get totalIDs > 1 > subTot > value, I don't know how to do it.

Here the ajax call:

$.ajax({
    url: url,
    type: "GET",
    dataType: 'json',
    success: function (data) {
        console.log(data.version);
        console.log(data.numberID);
        console.log(data.totalIDs); // problem is here, I don't know how to drill down further
    }
});

Any help is really appreciated. Thanks in advance. John

4

3 回答 3

0

如果你想得到一个特定的ID然后尝试

 data.totalIDs[0].subTot or data.totalIDs[1].subTotID[0]

或者你可以循环

 for(var i=0;i<data.totalIDs.length;i++)
 {
    console.log(data.totalIDs[i].subTot); 
    //another loop for subTotID
    for(var j=0;j<data.totalIDs[i].subTotID.length;j++)
    {
        console.log(data.totalIDs[i].subTotID[j]); 
    } 
 }
于 2013-09-20T10:32:47.930 回答
0

totalIDs 是一个数组,只有一个元素,一个结构。

该结构有两个由“1”和“2”索引的元素。

因此,您必须包括对数组和结构的引用。

data.totalIDs[0]["1"].subTot
于 2013-09-20T10:33:29.543 回答
0

你可以这样做:

$.ajax({
url: url,
type: "GET",
dataType: 'json',
success: function (data) {
    console.log(data.version);
    console.log(data.numberID);

    var length = data.totalIDs.length;
    for(i=0;i<length ;i++)
    {
      console.log(data.totalIDs[i]);
    }
});
于 2013-09-20T10:34:26.020 回答