user3671459
user3671459

Reputation: 455

typeMismatch jsonarray & jsonobject

I am getting 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":"[email protected]","name":"[email protected]"}] 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?

Upvotes: 0

Views: 4914

Answers (1)

Yazan
Yazan

Reputation: 6082

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);

Upvotes: 2

Related Questions