Skip to content

Instantly share code, notes, and snippets.

@mohanrajtrade
Forked from codebutler/JsonHelper.java
Created November 28, 2016 10:34
Show Gist options
  • Select an option

  • Save mohanrajtrade/7ba2b84ffff9d488a469bd8248136c08 to your computer and use it in GitHub Desktop.

Select an option

Save mohanrajtrade/7ba2b84ffff9d488a469bd8248136c08 to your computer and use it in GitHub Desktop.

Revisions

  1. @codebutler codebutler revised this gist May 10, 2012. 1 changed file with 31 additions and 2 deletions.
    33 changes: 31 additions & 2 deletions JsonHelper.java
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,33 @@
    import java.util.*;

    public class JsonHelper {
    public static Object toJSON(Object object) throws JSONException {
    if (object instanceof Map) {
    JSONObject json = new JSONObject();
    Map map = (Map) object;
    for (Object key : map.keySet()) {
    json.put(key.toString(), toJSON(map.get(key)));
    }
    return json;
    } else if (object instanceof Iterable) {
    JSONArray json = new JSONArray();
    for (Object value : ((Iterable)object)) {
    json.put(value);
    }
    return json;
    } else {
    return object;
    }
    }

    public static boolean isEmptyObject(JSONObject object) {
    return object.names() == null;
    }

    public static Map<String, Object> getMap(JSONObject object, String key) throws JSONException {
    return toMap(object.getJSONObject(key));
    }

    public static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap();
    Iterator keys = object.keys();
    @@ -24,12 +51,14 @@ public static List toList(JSONArray array) throws JSONException {
    }

    private static Object fromJson(Object json) throws JSONException {
    if (json instanceof JSONObject) {
    if (json == JSONObject.NULL) {
    return null;
    } else if (json instanceof JSONObject) {
    return toMap((JSONObject) json);
    } else if (json instanceof JSONArray) {
    return toList((JSONArray) json);
    } else {
    return json;
    }
    }
    }
    }
  2. @codebutler codebutler created this gist Apr 8, 2012.
    35 changes: 35 additions & 0 deletions JsonHelper.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.*;

    public class JsonHelper {
    public static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap();
    Iterator keys = object.keys();
    while (keys.hasNext()) {
    String key = (String) keys.next();
    map.put(key, fromJson(object.get(key)));
    }
    return map;
    }

    public static List toList(JSONArray array) throws JSONException {
    List list = new ArrayList();
    for (int i = 0; i < array.length(); i++) {
    list.add(fromJson(array.get(i)));
    }
    return list;
    }

    private static Object fromJson(Object json) throws JSONException {
    if (json instanceof JSONObject) {
    return toMap((JSONObject) json);
    } else if (json instanceof JSONArray) {
    return toList((JSONArray) json);
    } else {
    return json;
    }
    }
    }