Convert Json Into Map In Java Example

[Solved] Convert Json Into Map In Java Example | Java - Code Explorer | yomemimo.com
Question : convert json into map in java example

Answered by : pleasant-polecat-2473jm15r6n8

public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException { Map<String, Object> retMap = new HashMap<String, Object>(); if(json != JSONObject.NULL) { retMap = toMap(json); } return retMap;
}
public static Map<String, Object> toMap(JSONObject object) throws JSONException { Map<String, Object> map = new HashMap<String, Object>(); Iterator<String> 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<Object> toList(JSONArray array) throws JSONException { List<Object> list = new ArrayList<Object>(); for(int i = 0; i < array.length(); 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;
}

Source : https://stackoverflow.com/questions/21720759/convert-a-json-string-to-a-hashmap | Last Update : Sun, 16 Aug 20

Answers related to convert json into map in java example

Code Explorer Popular Question For Java