Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JSONObject获取JSONArray时出错_Java_Android_Json_Jsonobject_Android Json - Fatal编程技术网

Java 从JSONObject获取JSONArray时出错

Java 从JSONObject获取JSONArray时出错,java,android,json,jsonobject,android-json,Java,Android,Json,Jsonobject,Android Json,我试图从JSONObject获取JSONArray,但在LogCat中获取“无法创建JSONArray” 错误出现在此处: JSONArray array = response_object.getJSONArray("post"); 这是我的密码: try { int id; String author; String authorUniqueId; String authorPhotoUrl; String message; String c

我试图从JSONObject获取JSONArray,但在LogCat中获取“无法创建JSONArray”

错误出现在此处:

JSONArray array = response_object.getJSONArray("post");
这是我的密码:

try {
    int id;
    String author;
    String authorUniqueId;
    String authorPhotoUrl;
    String message;
    String createTime;

    JSONObject response_object = new JSONObject(response);
    JSONArray array = response_object.getJSONArray("post");

    for (int i = 0; i < array.length(); i++) {
        JSONObject jsonObject = array.getJSONObject(i);

        id = jsonObject.getInt("id");
        author = jsonObject.getString("author");
        authorUniqueId = jsonObject.getString("author_unique_id");
        authorPhotoUrl = jsonObject.getString("author_photo_url");
        message = jsonObject.getString("message");
        createTime = jsonObject.getString("created_at");

        Log.e("NewsActivity", "Added:" + author);


        data.add(new NewsDataModel(id, author, authorUniqueId, authorPhotoUrl, message, createTime));
    }
} catch (Exception e) {
    Log.e(TAG, "Can't create JSONArray");
}

您的JSON无效。看,它从这个
{“error”:false}
开始;右大括号将在1键结束你的
JSONObject
。@MohammedAoufZOUAG,噢,我没注意到!非常感谢你!还要更改这个int id=jsonObject.getInt(“id”);to Stirng id=jsonObject.getString(“id”)@MohammedAoufZOUAG,但id是int。@MrAwesome
id
目前无法触及。考虑先修复JSON。
{"error":false}
{"post":[
    {
        "id":"6",
        "author":"NAME..."
        "author_unique_id":"ID...",
        "message":"MESSAGE...",
        "created_at":"TIME...",
        "author_photo_url":"URL..."
    },

    {
        "id":"5",
        "author":"NAME..."
        "author_unique_id":"ID...",
        "message":"MESSAGE...",
        "created_at":"TIME...",
        "author_photo_url":"URL..."
    },

    {
        "id":"4",
        "author":"TEXT..."
        "author_unique_id":"ID...",
        "message":"MESSAGE...",
        "created_at":"TIME...",
        "author_photo_url":"URL..."
    }
]}