I got a little problem with getting my JSON Object.
try {
JSONObject jObject = new JSONObject( response.getBody());
JSONObject userObject = jObject.getJSONObject("data");
String nachricht = userObject.getString("nachricht");
String ausgeloest_von = userObject.getString("ausgeloest_von");
String erstellt_am = userObject.getString("erstellt_am");
I get the error at data of type org.json.JSONArray cannot be converted to JSONObject at the second line in my code snippet. I have the same code on a different API and its working.
{ "error": 200, "message": "Daten gefunden", "data": [ { "id": "105", "userid": "9981", "userid_notfall": "9985", "nachricht": "Notfall von Max", "ausgeloest_von": "", "status": "0", "erstellt_am": "2017-11-28 18:10:48", "aktualisiert_am": "" } ] }
This is what the response body looks like. I think the "[" brackets causing my problems. Any idea?
Thank you in advance!
Use this code:
JSONObject jObject = new JSONObject( response.getBody()); JSONArray userArray= jObject.getJSONArray("data");
for(int i =0;i < userArray.length();i++) {
JSONObject individualObject = userArray.getJSONObject(i);
String nachricht = userObject.getString("nachricht");
String ausgeloest_von = userObject.getString("ausgeloest_von"); String erstellt_am = userObject.getString("erstellt_am");
}