Last active
December 14, 2022 16:24
-
-
Save shortstuffsushi/37ecd21df9b6dca18a3e to your computer and use it in GitHub Desktop.
Simple Map/List to JSONObject/JSONArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Convert { | |
public static JSONObject toJson(Map<String, Object> map) { | |
JSONObject jsonObject = new JSONObject(); | |
for (String key : map.keySet()) { | |
try { | |
Object obj = map.get(key); | |
if (obj instanceof Map) { | |
jsonObject.put(key, toJson((Map) obj)); | |
} | |
else if (obj instanceof List) { | |
jsonObject.put(key, toJson((List) obj)); | |
} | |
else { | |
jsonObject.put(key, map.get(key)); | |
} | |
} | |
catch (JSONException jsone) { | |
Log.wtf("RequestManager", "Failed to put value for " + key + " into JSONObject.", jsone); | |
} | |
} | |
return jsonObject; | |
} | |
public static JSONArray toJson(List<Object> list) { | |
JSONArray jsonArray = new JSONArray(); | |
for (Object obj : list) { | |
if (obj instanceof Map) { | |
jsonArray.put(toJson((Map) obj)); | |
} | |
else if (obj instanceof List) { | |
jsonArray.put(toJson((List) obj)); | |
} | |
else { | |
jsonArray.put(obj); | |
} | |
} | |
return jsonArray; | |
} | |
public static Map<String, Object> fromJson(JSONObject jsonObject) { | |
Map<String, Object> map = new HashMap<String, Object>(); | |
Iterator<String> keyIterator = jsonObject.keys(); | |
while (keyIterator.hasNext()) { | |
String key = keyIterator.next(); | |
try { | |
Object obj = jsonObject.get(key); | |
if (obj instanceof JSONObject) { | |
map.put(key, fromJson((JSONObject) obj)); | |
} | |
else if (obj instanceof JSONArray) { | |
map.put(key, fromJson((JSONArray) obj)); | |
} | |
else { | |
map.put(key, obj); | |
} | |
} | |
catch (JSONException jsone) { | |
Log.wtf("RequestManager", "Failed to get value for " + key + " from JSONObject.", jsone); | |
} | |
} | |
return map; | |
} | |
public static List<Object> fromJson(JSONArray jsonArray) { | |
List<Object> list = new ArrayList<Object>(); | |
for (int i = 0; i < jsonArray.length(); i++) { | |
try { | |
Object obj = jsonArray.get(i); | |
if (obj instanceof JSONObject) { | |
list.add(fromJson((JSONObject) obj)); | |
} | |
else if (obj instanceof JSONArray) { | |
list.add(fromJson((JSONArray) obj)); | |
} | |
else { | |
list.add(obj); | |
} | |
} | |
catch (JSONException jsone) { | |
Log.wtf("RequestManager", "Failed to get value at index " + i + " from JSONArray.", jsone); | |
} | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this ignores any exceptions. This is because for my use case, I have a known, fixed type set. I don't expect exceptions to ever be thrown for failing to "get," so that failure is just swallowed and logged. This seems like a poor API choice, imo. The toJson could potentially throw with bad types, but since I know what types (primitives and maps) I'm putting in, I'm not too concerned.