JSON异常:java.lang.String无法转换为JSONObject

时间:2015-02-04 02:31:13

标签: android json

我正在抛出JSON异常而且我不知道为什么会抛出它。我已经看过几乎所有其他问题,就像我的一样,但我认为我的不同。所以我从网页上获取了一个JSONArray,它发送的JSON是有效的(我用验证器检查过它)。当我在JSONArray上执行getJSONObject时抛出异常。

这就是它所说的内容(个人信息已经加星标):

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

这是我的Java代码:

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

错误发生在我去的地方:

jobj = attendanceDetails.getJSONObject(i);

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

试试这个:

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