Java 从JSONObject获取JSONArray时出错
我试图从JSONObject获取JSONArray,但在LogCat中获取“无法创建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
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。@MrAwesomeid
目前无法触及。考虑先修复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..."
}
]}