繁体   English   中英

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

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

我正在抛出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);

任何帮助是极大的赞赏。

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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