Reputation: 91
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:
{"response":{"result":{"Contacts":{"row":[{"no":"1","FL":
[{"content":"1822766000000272057","val":"CONTACTID"},
{"content":"Lisa","val":"First Name"}]},{"no":"2","FL":
[{"content":"1822766000000119148","val":"CONTACTID"},
{"content":"Eric","val":"First
Name"}]}]}},"uri":"/crm/private/json/Contacts/searchRecords"}}
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:
Upvotes: 0
Views: 2167
Reputation: 1266
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...
}
Upvotes: 1