user3671459 user3671459 - 2 months ago 6
JSON Question

typeMismatch jsonarray & jsonobject

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

JSONObject
to a
JSONArray
.

i am downloading
JSON
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
JSON
.

However i get this error;

06-03 19:50:15.853: W/System.err(26492): org.json.JSONException: Value [{"friends":"HIDDENEMAIL@MAIL.com","name":"HIDDENEMAIL@MAIL.com"}] 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(JSON.java:111)
06-03 19:50:15.873: W/System.err(26492): at org.json.JSONObject.<init>(JSONObject.java:159)
06-03 19:50:15.883: W/System.err(26492): at org.json.JSONObject.<init>(JSONObject.java:172)
06-03 19:50:15.903: W/System.err(26492): at info.androidhive.jsonparsen.sharedUser$GetContacts.doInBackground(sharedUser.java:122)
06-03 19:50:15.913: W/System.err(26492): at info.androidhive.jsonparsen.sharedUser$GetContacts.doInBackground(sharedUser.java:1)
06-03 19:50:15.913: W/System.err(26492): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-03 19:50:15.923: W/System.err(26492): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-03 19:50:15.923: W/System.err(26492): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-03 19:50:15.933: W/System.err(26492): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-03 19:50:15.933: W/System.err(26492): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-03 19:50:15.933: W/System.err(26492): at java.lang.Thread.run(Thread.java:841)


the code to download the
JSON


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);
if(!emails.isEmpty()){
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
contactList.add(contact);

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

return null;
}


How can i correct this so it works?

Answer

if json string starts with [ then its array

if json string starts with { then its object

["friends": ===> is array

thus:

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