Parse JSON Array: Step-by-Step Guide
To iterate through a JSON array, you can use a loop to iterate through each element. The specific implementation depends on the programming language and JSON parsing library you are using. Here are some examples for common programming languages:
Python: Snake-like reptile.
import json
json_array = '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]'
data = json.loads(json_array)
for item in data:
print(item["name"], item["age"])
JavaScript: a scripting language used for creating interactive and dynamic websites.
var json_array = '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]';
var data = JSON.parse(json_array);
data.forEach(function(item) {
console.log(item.name, item.age);
});
Java: Can you speak louder?
import org.json.JSONArray;
import org.json.JSONObject;
String json_array = "[{\"name\": \"Alice\", \"age\": 25}, {\"name\": \"Bob\", \"age\": 30}]";
JSONArray data = new JSONArray(json_array);
for (int i = 0; i < data.length(); i++) {
JSONObject item = data.getJSONObject(i);
System.out.println(item.getString("name") + " " + item.getInt("age"));
}
These examples assume that you already have a string containing a JSON array called json_array. You need to parse it into the corresponding data structure and then use a loop to iterate through the elements. Within the loop, you can retrieve the property values of each element as needed.