Array indexes are zero-based: The first element in the array is 0, the second is 1, and so on.
JSON arrays are written inside square brackets.
Just like in JavaScript, an array can contain objects:
Example(1)
myObj = { "name":"Shiva", "age":30, "cars":[ "Ford", "BMW", "Fiat" ] };
Array Properties
Property | Description |
---|---|
constructor | Returns the function that created the Array object's prototype |
length | Sets or returns the number of elements in an array |
prototype | Allows you to add properties and methods to an Array object |
Complete Code For Accessing JSON Array Elements With Examples.
<!DOCTYPE html> <html> <head> <title>How Can I Access JSON Array Elements With Examples</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <br> <div class="text-center"> <h1 id="color" style="color: tomato">How Can I Access JSON Array Elements With Examples</h1> </div> <br> <div class="well"> <h2 id="demo1"></h2> <h2 id="demo2"></h2> </div> <script> var myObj, x,y; myObj = { "name":"Shiva", "age":30, "cars":[ "Ford", "BMW", "Fiat" ] }; x = myObj.cars; y = myObj.cars[0]; document.getElementById("demo1").innerHTML = x; document.getElementById("demo2").innerHTML = y; </script> </div> </body> </html>