I am getting a exception while working in json . My JSONPresr class is as follow
import java.io.BufferedReader;
import java.io.IOException;
import java.io
Learn from exception
org.json.JSONObject cannot be converted to JSONArray
You are getting this exception org.json.JSONObject cannot be converted to JSONArray
because you are trying to convert JSONObject to JSONArray
which is not possible.
{
represents json object node
[
represents json array node
your jsondata is json object format. change this line
JsonObject user_detail_jsonobj = json.getJSONObject(TAG_user_detail);
Its clear from error that you are trying to convert Json Object into Json array. That should not.
Here is the code to read your JSON response.
String json = "Assuming that here is your JSON response";
try {
JSONObject parentObject = new JSONObject(json);
JSONObject userDetails = parentObject.getJSONObject("user_details");
//And then read attributes like
String name = userDetails.getString("user_name");
String phone = userDetails.getString("user_phone");
String id = userDetails.getString("ref_id");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Above code is for {"user_details":{"user_id":"1","user_name":"chand","user_phone":"9620085675","ref_id":6386}}
JSON.