java将 json数组转map,例[{"key":"1"},{"key1":"2"}]转换成一个map
1个回答
展开全部
HashMap<String, String> map = new HashMap<String, String>();
String jsonStr="辩派[{\"key\兆洞":\"1\"},{\"key1\":\"2\族灶枯"}]";
JSONArray jsonArr=JSONArray.fromObject(jsonStr);
for(int i=0;i<jsonArr.size();i++){
JSONObject obj = JSONObject.fromObject(jsonArr.get(i));
Iterator it = obj.keys();
while (it.hasNext()){
String key = String.valueOf(it.next());
String value = (String) obj.get(key);
map.put(key, value);
}
}
System.out.println(map);
String jsonStr="辩派[{\"key\兆洞":\"1\"},{\"key1\":\"2\族灶枯"}]";
JSONArray jsonArr=JSONArray.fromObject(jsonStr);
for(int i=0;i<jsonArr.size();i++){
JSONObject obj = JSONObject.fromObject(jsonArr.get(i));
Iterator it = obj.keys();
while (it.hasNext()){
String key = String.valueOf(it.next());
String value = (String) obj.get(key);
map.put(key, value);
}
}
System.out.println(map);
追问
追答
因为KEY一样,第二次又被重新赋值了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询