gpt4 book ai didi

java - List> 到 org.json.JSONObject?

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:32 24 4
gpt4 key购买 nike

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();

map.put("abc", "123456");
map.put("def", "hmm");
list.add(map);
JSONObject json = new JSONObject(list);
try {
System.err.println(json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}

这段代码有什么问题?

输出是:

{"empty": false}

最佳答案

public String listmap_to_json_string(List<Map<String, Object>> list)
{
JSONArray json_arr=new JSONArray();
for (Map<String, Object> map : list) {
JSONObject json_obj=new JSONObject();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
try {
json_obj.put(key,value);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
json_arr.put(json_obj);
}
return json_arr.toString();
}

好吧,试试这个~这对我有用:D

关于java - List<Map<String,Object>> 到 org.json.JSONObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/512692/

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