View nested JSON objects

advertisements

I'm brand new to javascript and json and can't find any examples that explain how to get nested json objects.

This works great:

var someJson={"name":"spuder"};
alert(someJson.name);
//alert shows spuder

This returns undefined:

//This is the json from the stream I am reading
var someJson={"name":"spuder","car":[{"model":"ford","year":"2000"}] };
alert(?????);

how do you return spuders car model or car year?

alert(someJson.car.model); //doesn't work..


You have created your car as an array. To get the model of the first car in the array:

alert(someJson.car[0].model);

http://jsfiddle.net/YUrRg/