列表与LT;地图; LT;字符串,对象>>到org.json.JSONObject? [英] List<Map<String,Object>> to org.json.JSONObject?
本文介绍了列表与LT;地图; LT;字符串,对象>>到org.json.JSONObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
alright, try this~ This worked for me :D
这篇关于列表与LT;地图; LT;字符串,对象>>到org.json.JSONObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文