user3671459 user3671459 - 17 days ago 4x
JSON Question

typeMismatch jsonarray & jsonobject

i am getting the an error and i am not sure if i can just simply change my

to a

i am downloading
and want to get a value from it ( at the column friends). with this value i need to run a second url to download some other

However i get this error;

06-03 19:50:15.853: W/System.err(26492): org.json.JSONException: Value [{"friends":"","name":""}] of type org.json.JSONArray cannot be converted to JSONObject
06-03 19:50:15.863: W/System.err(26492): at org.json.JSON.typeMismatch(
06-03 19:50:15.873: W/System.err(26492): at org.json.JSONObject.<init>(
06-03 19:50:15.883: W/System.err(26492): at org.json.JSONObject.<init>(
06-03 19:50:15.903: W/System.err(26492): at info.androidhive.jsonparsen.sharedUser$GetContacts.doInBackground(
06-03 19:50:15.913: W/System.err(26492): at info.androidhive.jsonparsen.sharedUser$GetContacts.doInBackground(
06-03 19:50:15.913: W/System.err(26492): at android.os.AsyncTask$
06-03 19:50:15.923: W/System.err(26492): at
06-03 19:50:15.923: W/System.err(26492): at android.os.AsyncTask$SerialExecutor$
06-03 19:50:15.933: W/System.err(26492): at java.util.concurrent.ThreadPoolExecutor.runWorker(
06-03 19:50:15.933: W/System.err(26492): at java.util.concurrent.ThreadPoolExecutor$
06-03 19:50:15.933: W/System.err(26492): at

the code to download the

protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

Log.d("Response: ", "> " + jsonStr);

if (!jsonStr.isEmpty()) {
try {
//DEBUGER shows the following line is the line it catches an error and moves to JSONexception e;
JSONObject jsonObj = new JSONObject(jsonStr);
//TODO run this multiple times.
//TODO edit php colomn names
//TODO add fname and lname to database without losing the previous one.
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
JSONObject first = contacts.getJSONObject(0);
Log.d("response email ", first.getString("friends"));
String emails = first.getString("friends");
Log.i("check if empty", ""+emails);
url1 = "http://EXAMPLEURL.COM/API
ServiceHandler sh1 = new ServiceHandler();
String jsonStrShare = sh1.makeServiceCall(url1, ServiceHandler.GET);
JSONObject jsonObj1 = new JSONObject(jsonStrShare);
contacts1 = jsonObj1.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts1.length(); i++) {
JSONObject c = contacts1.getJSONObject(i);

String id = c.getString(TAG_FIRSTNAME);
String lastsharename = c.getString(TAG_LASTNAME);
String email = c.getString(TAG_EMAIL);
String list = c.getString(TAG_LIST);

// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();

// adding each child node to HashMap key => value
contact.put(TAG_FIRSTNAME, id);
contact.put(TAG_LASTNAME, lastsharename);
contact.put(TAG_EMAIL, email);
contact.put(TAG_LIST, list);
//contact.put(TAG_PHONE_MOBILE, mobile);

// adding contact to contact list

} catch (JSONException e) {
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");

return null;

How can i correct this so it works?


if json string starts with [ then its array

if json string starts with { then its object

["friends": ===> is array


JsonArray jsonArr = new JsonArray(json);
JsonObject jsonObj = jsonArr.getJSONObject(0);