Reputation: 23
I dont know why i'm having so much issues with parsing this simple json:
{"status":0,"result":{"success":false,"message":"Error"}}
How would I get the success and message String?
try {
river = response.getJSONArray("result");
// looping through All Contacts
for(int i = 0; i < river.length(); i++){
JSONObject c = river.getJSONObject(i);
if (c.has("message")) {
message = c.getString("message");
System.out.println("object_guid:"+message); }
if (c.has("success")) {
success = c.getString("success");
System.out.println("subtype:"+success); }
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 0
Views: 1068
Reputation: 23638
In your response your are not getting JSONArray
. It contains JSONObjects
only. So you need to get the JSONObject
not JSONArray
.
Try out as below:
river = response.getJSONObject("result");
if (river.has("message")) {
message = river.getString("message");
System.out.println("object_guid:"+message); }
if (river.has("success")) {
success = river.getString("success");
System.out.println("subtype:"+success); }
}
Upvotes: 0
Reputation: 7415
Your result
is not jsonarray, its an object.
{"status":0,"result":{"success":false,"message":"Error"}}
JSONObject river = response.getJSONObject("result");
String success = river.getString("success");
String message = river.getString("message");
Upvotes: 2
Reputation: 13520
Replace
river = response.getJSONArray("result");
with
river = response.getJSONObject("result");
The result
tag is a JSONObject
and not a JSONArray
Upvotes: 2