JAVA中JSONObject对象和Map对象之间的相互转换
JSONObject转换成Map对象
String result = sendRequest(jsonObject.toString(), 2L, url); //json字符串 JSONObject res = JSONObject.fromObject(result); if ("true".equals(res.getString("status"))) { JSONObject data = res.getJSONObject("data"); Iterator it = data.keys(); // 遍历jsonObject数据,添加到Map对象 while (it.hasNext()){
//通过key获取value String key = String.valueOf(it.next()); Double value = data.getDouble(key); map.put(key, value); } }
//第二种 效率更高,key和value同时获取,适用于大数据量 Iterator it =data.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next(); map.put(entry.getKey(), entry.getValue()); }