0

I'm having a JSON Exception being thrown and I don't know why it is being thrown. I've taken a look at almost all of the other questions that are just like mine, but I think mine is different. So I am getting a JSONArray from a webpage, and the JSON it sends is valid (I checked it with a validator). The exception is being thrown when I perform getJSONObject on the JSONArray.

Here's exactly what it says (personal information is starred out):

org.json.JSONException: Value  [{"name":"*****","profilePicture":"*****"}] at 0 of type java.lang.String cannot be converted to JSONObject

And here's my Java code:

 protected Void doInBackground(JSONObject... params) {
        JSONObject jsonObject = params[0];
        ClientServerInterface clientServerInterface = new ClientServerInterface();
        JSONArray attendanceDetails = clientServerInterface.postData("http://54.164.136.46/get_attendance.php", jsonObject);
        Log.e("Attendance Details: ", attendanceDetails.toString());

        nameAttendees = new String[attendanceDetails.length()];
        pictureAttendees = new String[attendanceDetails.length()];

        JSONObject jobj = null;
 for(int i = 0; i < attendanceDetails.length(); i++)
        {
            try {
                jobj = attendanceDetails.getJSONObject(i);
                nameAttendees[i] = jobj.getString("name");
                pictureAttendees[i] = jobj.getString("profilePicture");
            } catch (JSONException e) {
                e.printStackTrace();
            }

The error is happening at the line where I go:

jobj = attendanceDetails.getJSONObject(i);

Any help is greatly appreciated.

6
  • This code seem correct. Can you post few more line above the for loop? Commented Feb 4, 2015 at 2:40
  • Can you post the JSON you are trying to parse? It looks like the element you try to parse is a string ["name"] while you are expecting an object [{"name": "name"}] Commented Feb 4, 2015 at 2:43
  • The JSON is in the OP in the exception Commented Feb 4, 2015 at 2:45
  • Added code above loop Commented Feb 4, 2015 at 2:46
  • Oh I see something strange. I printed out the JSONArray and here is what it is: [" [{\"name\":\"*****\",\"profilePicture\":\"*****\"}]\n"] Commented Feb 4, 2015 at 2:50

1 Answer 1

1

Try this:

for(int i = 0; i < attendanceDetails.length(); i++)
    {
        try {
            JSONArray hello = new JSONArray(attendanceDetails.getJSONArray(i));
            JSONObject jObject = new  JSONObject(hello.get(i).toString());
            nameAttendees[i] = jObject.getString("name");
            pictureAttendees[i] = jObject.getString("profilePicture");
        } catch (JSONException e) {
            e.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

11 Comments

The variable jobj is of type JSONObject. So should I cast it to JSONObject, since .get returns type Object?
Now I'm getting this exception: java.lang.String cannot be cast to org.json.JSONObject
You can't.when you use get function.You can get a String in your example .though the String is a object.It can's convert to JSONObject,unless it is a real object instead of subclass of object.
Here's what I did: JSONObject jObject = new JSONObject(attendanceDetails.get(i).toString()); nameAttendees[i] = jObject.getString("name"); pictureAttendees[i] = jObject.getString("profilePicture"); And error is JSONArray cannot be converted into JSONObject
JSONArray hello = new JSONArray(attendanceDetails.getJSONArray(i));JSONObject jObject = new JSONObject(hello.get(i).toString());
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.