Abhijit Gujar
Abhijit Gujar

Reputation: 443

How to detect different json object?

If my query is successful then my php server returns json object like this

{"Success":[
{"firstName":"John", "lastName":"Doe"}, 
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}

if it fails the it returns jsonobject like this

{"fail":[
{"msg":"Fail reason"}
]}

i want to detect "success" and "fail" in android . I tried this, its not working, any better solution is welcome ...

JSONObject jsonResponse     
if(jsonResponse.getBoolean("orderid"))

Upvotes: 1

Views: 61

Answers (2)

SorryForMyEnglish
SorryForMyEnglish

Reputation: 1181

check the key you can:

JSONObject jsonResponse;   
if(jsonResponse.has("Success")){
  JSONArray data = jsonResponse.optJSONArray("Success");
  //*********
}else{
  String error = jsonResponse.optJSONArray("fail").optJSONObject(0).optString("msg");
} 

Upvotes: 1

msvetterracer93
msvetterracer93

Reputation: 21

Change

if(jsonResponse.getBoolean("orderid"))

to

if(jsonResponse.has("Success"))

Upvotes: 1

Related Questions