普通的Json字符串到HashMap

json to HashMap转换json to HashMap有很多问题。 我希望它对每个人都有帮助。

以下代码将直接值或值Array转换为HashMap

//递归调用函数

  private static Map getMap(JSONObject object, String json) throws Exception { Map map = new HashMap(); Object jsonObject = null; Iterator keys = object.keys(); while (keys.hasNext()) { String key = keys.next(); Object value = object.get(key); if (value instanceof JSONObject) { map.put(key, getMap((JSONObject) value, json)); continue; } // If value is in the form of array if (value instanceof JSONArray) { JSONArray array = ((JSONArray) value); List list = new ArrayList(); for (int i = 0 ; i < array.length() ; i++) { jsonObject = array.get(i); if (jsonObject instanceof JSONObject) { list.add(getMap((JSONObject) jsonObject, json)); } else { list.add(jsonObject); } } map.put(key, list); continue; } map.put(key, value); } return map; } 

//调用方法

 public static Map convertJsonToMap(String json) { Map map = new HashMap(); JSONObject jsonObject = null; try { if (null != json) { jsonObject = new JSONObject(json); map = getMap(jsonObject, json); } } catch (Exception e) { throw new SystemException("Unable to read JSOn Object"); // TODO : Handle Exception } return map; }