Namo
Namo

Reputation: 157

How to parse JSON String in Android..?

Hi All I'm calling one php script from my Android code to insert record into the database. On successful insert I'm getting one string in following format-

{"success":1,"message":"Member registered successfully."}

And in case of error I'm getting the following string-

{"success":0,"message":"Oops! An error occurred."}

Now I wants to parse that string to check whether record is inserted successfully or not for that I have tried following code

JSONArray jsonarray = new JSONArray(response);
JSONObject jsonobj = jsonarray.getJSONObject(0);
String strResp=jsonobj.getString("success");

but strResp is getting null..! Please help. Thank you..!

Upvotes: 0

Views: 87

Answers (3)

chiragkyada
chiragkyada

Reputation: 3615

{ } means json object...and [  ] means json array..

here, {"success":1,"message":"Member registered successfully."} is json object...

so,

JSONObject jsonobj = jsonarray.getJSONObject(response);
String  strResp=jsonobj.getString("success");

Upvotes: 0

Giru Bhai
Giru Bhai

Reputation: 14408

Try this

JSONObject jObj = new JSONObject(response);
String strResp = String.valueOf(jObj.getInt("success"));

Because success in your json response is int.

Upvotes: 0

joselufo
joselufo

Reputation: 3415

The code.

JSONObject jObj = new JSONObject(response);
String strResp = jObj.getString("success");

Upvotes: 1

Related Questions