如何读取嵌套数组

时间:2014-06-20 15:51:09

标签: jquery

我已将此作为回复

[
    {
        "name": "Large, 100 Ml",
        "image": "http://hostip:8080/OrderSnacks/JSON_images/icecream_cup_vanilla.jpg",
        "quantity": "1",
        "price": "75",
        "toppings": []
    },
    {
        "name": "Regular, 50 Ml",
        "image": "http://hostip:8080/OrderSnacks/JSON_images/icecream_cup_vanilla.jpg",
        "quantity": "2",
        "price": "150",
        "toppings": [
            {
                "name": "Regular, 50 Ml0",
                "value": [
                    "Honey with Chocolate Sauce  10 ML"
                ]
            },
            {
                "name": "Regular, 50 Ml1",
                "value": [
                    "Honey with Chocolate Sauce  10 ML",
                    "Honey with Carmel  10 ML"
                ]
            }
        ]
    }
]

我如何阅读toppings数组值?

我试着用这种方式阅读

for (var n = 0; n < toppins.values.length; n++) 
{
alert(toppins.values[n]);
}

但是它给errror无法读取未定义的属性

任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

Demo Fiddle

Javascript代码:

for (var i = 0; i < json.length; i++) {
    var obj = json[i].toppings;
    for (var j = 0; j < obj.length; j++) {
        alert(obj[j].value);
    }
}

答案 1 :(得分:0)

根据你的JSON,你有一个错字,value不是valuestoppings不是toppins

for (var n = 0; n < toppings.value.length; n++) 
{
alert(toppings.value[n]);
}