Skip to content Skip to sidebar Skip to footer

String Cannot Be Converted To Jsonobject Exception

Newbie to Json here, I'm gettin a Value Database of type java.lang.String cannot be converted to JSONObject error when I try and run the following code.I cant make any sense of ot

Solution 1:

The Json you are posting is invalid, trim that {"server_response and closing curly braces too. The remaining Json will be like this, which is valid now,

[{"code":"reg_true","message":"Sucessful registration.Thank you.Enjoy"}]

The you can parse it like here,

JSONArray jsonarray = newJSONArray(json);
JSONObject jsonobject = jsonarray.getJSONObject(0);
String code = jsonobject.getString("code");
String message = jsonobject.getString("message");

UPDATE :

If your Json is as you commented then you can easily retrieve this like

JSONObject jobject = newJSONObject(json);
JSONArray jsonarray = jobject.getJSONArray("server_response");
JSONObject jsonobject = jsonarray.getJSONObject(0);
String code = jsonobject.getString("code");
String message = jsonobject.getString("message");

Solution 2:

Your json is invalid. You can check your json on jsonlint.com Correct json should be like this {"server_response":{"code":"reg_true","message":"Sucessful registration.Thank you.Enjoy"}} Since you are using only one json object there is no need to create json array.

Remember if it starts with [ and ends with ] its json array; with curly braces {...} its json object.

Post a Comment for "String Cannot Be Converted To Jsonobject Exception"