How can we iterate through a JSONArray in Java to retrieve a specific field’s value?
To iterate over a JSONArray and retrieve the value of a specific field, you can use the following method:
// 假设jsonArray是你的JSONArray对象
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// 获取某个字段的值,比如字段名为"field"
String fieldValue = jsonObject.getString("field");
// 打印该字段的值
System.out.println("Field value: " + fieldValue);
}
In this example, we are using a for loop to iterate through each JSONObject in a JSONArray and retrieve the value of a specific field using the getString() method. You can obtain the corresponding value based on the specific field name and perform the necessary operations.