A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.
Example :
<!DOCTYPE HTML> <html> <head> <title>JSON</title> </head> <script> function func() { var data = { // starting first array data code : 42, items : [ // starting second array items { id : 1, name : 'foo' }, { id : 2, beverages : // starting third array beverages [ { soft : 'cold drink', hot : 'tea' }, { refreshing : 'onjuice', forGeeks : 'you need lot of coffee' } ] // ending beverages } // ending second element of item ] // ending item array }; // ending data /***********setting values************/ // will output : foo document.getElementById("id2").innerHTML = data.code; // will output : 42 document.getElementById("id1").innerHTML = data.items[0].name; // will output : 2 document.getElementById("id3").innerHTML = data.items[1].id; // will output : you need lot of coffee document.getElementById("id4").innerHTML = data.items[1].beverages[1].forGeeks; /********ending setting values*********/ } // ending JavaScript function </script> <body onLoad="func()"> <p id="id1"></p> <p id="id2"></p> <p id="id3"></p> <p id="id4"></p> </body> </html>
Output :
foo 42 2 you need lot of coffee