Unlikely Legends Media Unlikely Legends Media - 1 year ago 81
JSON Question

How to parse JSON in Android Studio

I am trying to figure out how to parse data that comes from Zoho CRM API inside of Android Studio. I am relatively new, but I do know how to parse data from a JSON response like this:

"Data": [
{ "subdata": "data"

Something kind of like that I can parse no problem in Android Studio, even with multiple subdata points, it's not that hard. But, I am at a complete loss when it comes to parsing data that looks like this:

{"content":"Lisa","val":"First Name"}]},{"no":"2","FL":

Does anyone know how to parse data like this inside of Android Studio?

Update: I have a photo of what the JSON looks like in Json Viewer:

enter image description here

Answer Source

Just take it layer by layer. It can get a little verbose so I like to have a class called JSONUtils or something and use convenience methods like this to help parsing JSON without having to wrap everything in try-catch blocks:

 * Retrieves a json object from the passed in json object.
 * @param json The json object from which the returned json object will be retrieved.
 * @param key The key whose value is the json object to be returned.
 * @return A json object.
 * */
public static JSONObject jsonObjectFromJSONForKey(JSONObject json, String key) {
    try { 
        return json.getJSONObject(key); 
    catch (JSONException e) { 
        return null; 

You can make variations of this for any other data types, and by doing so you can just have your try-catch blocks in one area, and just check for null when invoking these kind of methods.

JSONObject responseJSON = JSONUtils.jsonObjectFromJSONForKey(json, "response");
if (responseJSON != null) {
    JSONObject resultJSON = JSONUtils.jsonObjectFromJSONForKey(responseJSON, "result");
    // So on and so forth...