Array in JSON
Advertisements
Array in JSON
In JSON array are declare inside square brackets. Some like JavaScript, a JSON array can contain multiple objects.
Example
"employees":[ {"firstName":"Komal", "lastName":"Pandit"}, {"firstName":"Ajay", "lastName":"Jain"}, {"firstName":"Hitesh","lastName":"Kumar"} ]
In the example above, the object "employees" is an array containing three objects. Each object is a record of a person (with a first name and a last name).
Declare array in JSon
Example
var employees = [ {"firstName":"Komal", "lastName":"Pandit"}, {"firstName":"Ajay", "lastName":"Jain"}, {"firstName":"Hitesh","lastName":"Kumar"} ];
Access Data for array in JSON
First way
employees[0].firstName + " " + employees[0].lastName; // returns Komal Pandit
Second Way
employees[0]["firstName"] + " " + employees[0]["lastName"]; // returns Komal Pandit
Modify Data of array in JSON
First way
employees[0].firstName = "Kajol";
Google Advertisment