Skip to content

Instantly share code, notes, and snippets.

@codebutler
Created April 8, 2012 20:20
Show Gist options
  • Save codebutler/2339666 to your computer and use it in GitHub Desktop.
Save codebutler/2339666 to your computer and use it in GitHub Desktop.
Convert Android JSONObject/JSONArray to a standard Map/List.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
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();
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 == 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;
}
}
}
Copy link

ghost commented Dec 14, 2012

Brilliant! Thanks a ton!

@strokine
Copy link

strokine commented Jan 5, 2013

Thanks for the code.
But shouldn't the line 19 be:
json.put(toJSON(value));
?

@edgurgel
Copy link

LICENSE? :)

@codebutler
Copy link
Author

This can just be considered public domain... I recommend using Gson instead.

@chandjava
Copy link

Thank you very much to you. this code very useful to me

@renanalmeida
Copy link

Thank you!

@OneCricketeer
Copy link

Thanks!

@mlakhia
Copy link

mlakhia commented Apr 18, 2014

Exactly what I was looking for. Thank you!

@debugger22
Copy link

How to use this?

@Mukes
Copy link

Mukes commented Sep 16, 2014

Doesn't line 59
toList((JSONArray) json);
gives ClassCastException.

@MarwanT
Copy link

MarwanT commented Jun 8, 2015

As Strokine said,

Line 19 should be:
json.put(toJSON(value));

@marlonla2
Copy link

/*
  * put string into file jsonFileArr.json
 * [{"username":"Hello","email":"hello@email.com","credits"
 * :"100","twitter_username":""},
 * {"username":"Goodbye","email":"goodbye@email.com"
 * ,"credits":"0","twitter_username":""},
 * {"username":"mlsilva","email":"mlsilva@email.com"
 * ,"credits":"524","twitter_username":""},
 * {"username":"fsouza","email":"fsouza@email.com"
 * ,"credits":"1052","twitter_username":""}]
 */

public class TestaGsonLista {

public static void main(String[] args) {
Gson gson = new Gson();
 try {
    BufferedReader br = new BufferedReader(new FileReader(
            "C:\\Temp\\jsonFileArr.json"));
    JsonArray jsonArray = new JsonParser().parse(br).getAsJsonArray();
    for (int i = 0; i < jsonArray.size(); i++) {
        JsonElement str = jsonArray.get(i);
        Usuario obj = gson.fromJson(str, Usuario.class);
        //use the add method from the list and returns it. Or map methods.
        System.out.println(obj);
        System.out.println(str);
        System.out.println("-------");
    }
 } catch (IOException e) {
    e.printStackTrace();
 }
}

@rajeev001
Copy link

Thank you
, it's very useful

@eniz1806
Copy link

Can you someone tell me how can I get this and parse this,and put it in list and show it to listView?

{"country":[[8,"Bosnia and Herzegovina","ba","BiH"]],"cities":[[8,"Bosnia and Herzegovina","ba","BiH","Sarajevo"]],"properties":[["Hotel Sokak","Bosnia and Herzegovina","ba","BiH","Sarajevo","7c6d2221353045408d8bbe8252bebd34"]]}

@KalaiSelvanG
Copy link

License?

@tahakorkem
Copy link

Useful!

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