To read a list of JSON objects in JavaScript, you can use the JSON.parse()
function to parse the JSON string into a JavaScript object or an array.
Here's an example:
var jsonString = '[{"name":"Maximus","age":30},{"name":"Peter Parker","age":25},{"name":"Bob Krammer","age":40}]'; // Parse the JSON string into an array of objects var jsonArray = JSON.parse(jsonString); // Iterate over the array and access the properties of each object for (var i = 0; i < jsonArray.length; i++) { var obj = jsonArray[i]; console.log("Name: " + obj.name + ", Age: " + obj.age); }
In the above example, the jsonString
variable holds a JSON string representing an array of objects. The JSON.parse()
function is used to parse the JSON string into the jsonArray
variable, which becomes an array of objects.
You can then iterate over this array and access the properties of each object as shown in the for
loop.
Note that the JSON string should be well-formed, with double quotes around property names and string values.
Hope this helps!