JSONObject output = errcode0pagerTotal2Limit10Offset0data_id14a8d65e7a673date2023-07-27 091548describe外包imageshttpmyImage2023-7-2780jpghttpmyImage2023-7-271690420548664_2141jpgusername_id8ecffd7065f9e
你可以使用以下代码来提取data中每个id下的images:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
String jsonString = "{\"errcode\":0,\"pager\":{\"Total\":2,\"Limit\":10,\"Offset\":0},\"data\":[\"{\\\"_id\\\":\\\"14a8d65e7a673\\\",\\\"date\\\":\\\"2023-07-27 09:15:48\\\",\\\"describe\\\":\\\"外包\\\",\\\"images\\\":[\\\"http://myImage/2023-7-27/80.jpg\\\",\\\"http://myImage/2023-7-27/1690420548664_2141.jpg\\\"],\\\"username\\\":\\\"\\\"}\",\"{\\\"_id\\\":\\\"8ecffd7065f9eca\\\",\\\"date\\\":\\\"2023-08-23 10:14:37\\\",\\\"describe\\\":\\\"测试\\\",\\\"images\\\":[\\\"http://myImage/2023-8-23/20.jpg\\\"],\\\"openid\\\":\\\"username\\\":\\\"admin\\\"}\"],\"errmsg\":\"ok\"}";
try {
JSONObject output = new JSONObject(jsonString);
JSONArray data = output.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
String item = data.getString(i);
JSONObject itemObject = new JSONObject(item);
String id = itemObject.getString("_id");
JSONArray images = itemObject.getJSONArray("images");
System.out.println("ID: " + id);
System.out.println("Images: " + images.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
运行以上代码将输出每个id及其对应的images。
输出结果为:
ID: 14a8d65e7a673
Images: ["http://myImage/2023-7-27/80.jpg","http://myImage/2023-7-27/1690420548664_2141.jpg"]
ID: 8ecffd7065f9eca
Images: ["http://myImage/2023-8-23/20.jpg"]
``
原文地址: https://www.cveoy.top/t/topic/iLFu 著作权归作者所有。请勿转载和采集!