Skip to content

Instantly share code, notes, and snippets.

@shortstuffsushi
Last active December 14, 2022 16:24
Show Gist options
  • Save shortstuffsushi/37ecd21df9b6dca18a3e to your computer and use it in GitHub Desktop.
Save shortstuffsushi/37ecd21df9b6dca18a3e to your computer and use it in GitHub Desktop.
Simple Map/List to JSONObject/JSONArray
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;
}
}
@bharal
Copy link

bharal commented Mar 27, 2018

imports?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment