javascript
java将json转为hashmap_将JSON字符串转换为HashMap
largeQ
幾天前我通過遞歸編寫了這段代碼。public?static?Map?jsonToMap(JSONObject?json)?throws?JSONException?{
????Map?retMap?=?new?HashMap();
????if(json?!=?JSONObject.NULL)?{
????????retMap?=?toMap(json);
????}
????return?retMap;}public?static?Map?toMap(JSONObject?object)?throws?JSONException?{
????Map?map?=?new?HashMap();
????Iterator?keysItr?=?object.keys();
????while(keysItr.hasNext())?{
????????String?key?=?keysItr.next();
????????Object?value?=?object.get(key);
????????if(value?instanceof?JSONArray)?{
????????????value?=?toList((JSONArray)?value);
????????}
????????else?if(value?instanceof?JSONObject)?{
????????????value?=?toMap((JSONObject)?value);
????????}
????????map.put(key,?value);
????}
????return?map;}public?static?List?toList(JSONArray?array)?throws?JSONException?{
????List?list?=?new?ArrayList();
????for(int?i?=?0;?i?
????????Object?value?=?array.get(i);
????????if(value?instanceof?JSONArray)?{
????????????value?=?toList((JSONArray)?value);
????????}
????????else?if(value?instanceof?JSONObject)?{
????????????value?=?toMap((JSONObject)?value);
????????}
????????list.add(value);
????}
????return?list;}
總結
以上是生活随笔為你收集整理的java将json转为hashmap_将JSON字符串转换为HashMap的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 纯数字版MSN账号
- 下一篇: murmurhash2算法 和 DJB