如何在json数据返回中访问嵌套数组?

时间:2014-10-18 00:19:58

标签: jquery json

这是前一个问题的一个非常轻微的重点,但有一个重要区别。我有一个json数据返回如下:

{
    "api_id": "89881d38-5657-11e4-a2d1-22000ac5040c",
    "country": "Czech Republic",
    "country_code": 420,
    "country_iso": "CZ",
    "message": {
        "inbound": {
            "rate": "0.00000"
        },
        "outbound": {
            "rate": "0.05900"
        },
        "outbound_networks_list": [
            {
                "group_name": "Czech Republic-Telefonica O2",
                "rate": "0.06800"
            },
            {
                "group_name": "Czech Republic-T-Mobile ",
                "rate": "0.06800"
            },
            {
                "group_name": "Czech Republic-U:fon",
                "rate": "0.05900"
            },
            {
                "group_name": "Czech Republic-Vodafone ",
                "rate": "0.05900"
            }
        ]
    },
    "phone_numbers": {
        "local": {
            "rate": "0.80000"
        },
        "tollfree": {
            "rate": null
        }
    },
    "voice": {
        "inbound": {
            "ip": {
                "rate": "0.00300"
            },
            "local": {
                "rate": "0.00500"
            },
            "tollfree": {
                "rate": null
            }
        },
        "outbound": {
            "ip": {
                "rate": "0.00300"
            },
            "local": {
                "rate": "0.02940"
            },
            "rates": [
                {
                    "prefix": [
                        "420608",
                        "4207040",
                        "4207047",
                        "42077",
                        "420967"
                    ],
                    "rate": "0.02940"
                },
                {
                    "prefix": [
                        "420601",
                        "420602",
                        "420603",
                        "420604",
                        "420605",
                        "420606",
                        "420607",
                        "420702",
                        "420703",
                        "42070300",
                        "420705",
                        "42072",
                        "42073",
                        "42077273",
                        "42079",
                        "42079083",
                        "4207920",
                        "4207921",
                        "4207922",
                        "4207923",
                        "4207924",
                        "4207925",
                        "42079950",
                        "42079951",
                        "420840",
                        "420841",
                        "420842",
                        "420847",
                        "420848",
                        "420849",
                        "42093",
                        "420961",
                        "420962",
                        "420963",
                        "420964",
                        "420965",
                        "420966"
                    ],
                    "rate": "0.03010"
                },
                {
                    "prefix": [
                        "420",
                        "4202"
                    ],
                    "rate": "0.03930"
                }
            ],
            "tollfree": {
                "rate": null
            }
        }
    }
}

我可以使用以下代码访问“phone_numbers.voice.outbound.local.rate”:

$.each( data, function( key, value ) {
  //code omitted for convenience
  txt += '<p>Make calls : '+ round((parseFloat(data.voice.outbound.local.rate * 100) + 3.0),2) +' cents per minute</p>';
 }

但是当我将相同的逻辑应用于出现“前缀”位的嵌套数组时,我得到一个未定义的错误消息,所以:

txt += '<p>Prefix: '+ data.["rates"]["prefix"] +'</p>';

返回“undefined”,类似地:

txt += '<p>Prefix: '+ data.rates.prefix +'</p>';

也失败了。我希望能够访问json返回的那一部分并迭代每个前缀数字并打印它旁边的速率。所以我们有,例如:

<div>
  <p> Prefix: 420608 Rate: 0.02940 </p>
</div>

一如既往,所有的帮助都感激不尽。谢谢。

1 个答案:

答案 0 :(得分:1)

这是data.voice.outbound.rates。由于这是一个对象数组,您需要迭代它。然后prefix也是一个数组,所以你需要一个嵌套循环:

$.each(data.voice.outbound.rates, function(i, prefix_rate) {
    var rate = prefix_rate.rate;
    $.each(prefix_rate.prefix, function(j, prefix) {
        txt += '<p>Prefix: ' + prefix + ' Rate: ' + rate + '</p>';
    });
});