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());
}

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM