简体   繁体   中英

JSON Exception: java.lang.String cannot be converted to JSONObject

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM