I have an API Output like this:
{"user" : {"status" : {"stat1" : "54", "stats2" : "87"}}}
I create a simple JSONObject from this API with:
JSONObject json = getJSONfromURL(URL);
After this I can read the data for User like this:
String user = json.getString("user");
But how do I get the Data for stat1 and stat2?
JSONObjectprovides accessors for a number of different data types, including nestedJSONObjectsandJSONArrays, usingJSONObject.getJSONObject(String),JSONObject.getJSONArray(String).Given your JSON, you’d need to do something like this:
Note the lack of error handling here: for instance the code assumes the existence of the nested members – you should check for
null– and there’s no Exception handling.