从JSONObject获取JSONArray时出错

时间:2016-02-06 15:38:41

标签: 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 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}
{"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..."
    }
]}

0 个答案:

没有答案