gpt4 book ai didi

java - 如何获取 List 中的每个元素 json = new ArrayList()

转载 作者:行者123 更新时间:2023-12-01 18:00:31 31 4
gpt4 key购买 nike

我需要获取 ArrayList<JSONObject>()中的值 ArrayList<List<String>>()

ArrayList<List<String>>为例我使用了以下代码

for(List<String> dreport : report)  {
for(String ddreport: dreport) {
// CODE GOES HERE
}
}

在新的场景中,我需要使用该功能。但该值包含在 ArrayList() 中,而不是字符串列表的列表

示例代码:

List<JSONObject> json = new ArrayList<JSONObject>();
JSONObject json_data1 = new JSONObject();
json_data1.put("count", "value");
json_data1.put("count", "value");
json_data1.put("count", "value");
json.add(json_data1);

最佳答案

ArrayList<JSONObject> jsonObjects = new ArrayList<JSONObject>();
//ADD objects in the jsonObjects

ArrayList<List<String>> jsonObjectsResultData = new ArrayList<>();
//Loop for the all JSONObject
for (JSONObject jsonObject : jsonObjects){
Iterator<String> keys= jsonObject.keys();
List<String> jsonObjectsValues = new ArrayList<String>();
//Loop for the JSONObject keys and values
while (keys.hasNext())
{
try{
String keyValue = (String)keys.next();
String valueString = jsonObject.getString(keyValue);
jsonObjectsValues.add(valueString);
} catch (Exception e){
e.printStackTrace();
}
}
jsonObjectsResultData.add(jsonObjectsValues);
}
//Your result array list
Log.i("RESULT DATA >>", jsonObjectsResultData.toString());

关于java - 如何获取 List<JSONObject> 中的每个元素 json = new ArrayList<JSONObject>(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41344960/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com