Pawan
Pawan

Reputation: 32331

How to read nested array

I have got this as response

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

How can i read the toppings array values ??

I tried to read this way

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

But it is giving errror can't read property of undefined

could anybody please help me on this .

Upvotes: 0

Views: 55

Answers (2)

Bhavik
Bhavik

Reputation: 4904

Demo Fiddle

Javascript code:

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);
    }
}

Upvotes: 1

tymeJV
tymeJV

Reputation: 104795

You have a typo, it's value not values and toppings not toppins - per your JSON:

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

Upvotes: 0

Related Questions