Android - Retrofit json解析
我有以下的json数组。我想用改装Android - Retrofit json解析
[
{
"todo_id": "1",
"todo_content": "Homework",
"date": "2016-05-05",
"iscomplete": "1",
"imagelink": "Lighthouse.jpg"
},
{
"todo_id": "2",
"todo_content": "exam",
"date": "2015-04-21",
"iscomplete": "0",
"imagelink": "Desert.jpg"
},
{
"todo_id": "3",
"todo_content": "Lab report",
"date": "2014-08-29",
"iscomplete": "1",
"imagelink": "FB_IMG_14700753538617403.jpg"
}
]
当我使用JSON解析改造解析它在android系统,但它不解析todo_id和todo_content关键值,其中,因为它表明其他键值日期,iscomplete和ImageLink的 这是我的android代码改造响应
public void onResponse(Call<List<AndroidVersion>> call, Response<List<AndroidVersion>> response) {
if (response.isSuccess()) {
try {
for (int i = 0; i < response.body().size(); i++) {
AndroidVersion ver = new AndroidVersion();
ver.setTodoContent(response.body().get(i).getTodoContent());
ver.setTodoId(response.body().get(i).getTodoId());
ver.setDate(response.body().get(i).getDate());
ver.setIscomplete(response.body().get(i).getIscomplete());
ver.setImagelink(response.body().get(i).getImagelink());
arrayList.add(ver);
Log.v("ret",response.body().get(i).toString());
}
adapter = new DataAdapter(arrayList);
recyclerView.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Example {
@SerializedName("todo_id")
@Expose
private String todoId;
@SerializedName("todo_content")
@Expose
private String todoContent;
@SerializedName("date")
@Expose
private String date;
@SerializedName("iscomplete")
@Expose
private String iscomplete;
@SerializedName("imagelink")
@Expose
private String imagelink;
public String getTodoId() {
return todoId;
}
public void setTodoId(String todoId) {
this.todoId = todoId;
}
public String getTodoContent() {
return todoContent;
}
public void setTodoContent(String todoContent) {
this.todoContent = todoContent;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getIscomplete() {
return iscomplete;
}
public void setIscomplete(String iscomplete) {
this.iscomplete = iscomplete;
}
public String getImagelink() {
return imagelink;
}
public void setImagelink(String imagelink) {
this.imagelink = imagelink;
}
}
它应该为你工作
我错了Pojo类 –
使用序列化名称。
您的JSON响应为参数名“todo_id”所以在你的POJO类,你就不得不提到它,因为它是按照POJO类的例子:
public class Pojo {
private int todo_id;
private String todo_content;
public int getTodo_id() {
return todo_id;
}
public void setTodo_id(int todo_id) {
this.todo_id = todo_id;
}
public String getTodo_content() {
return todo_content;
}
public void setTodo_content(String todo_content) {
this.todo_content = todo_content;
}
}
名称应匹配。希望这有助于!
哦,现在我想通了我的错误 –
很棒... !!!您可以在andorid studio itslef中使用GSON Format插件,它会以序列化格式创建pojo类。 – Mrinmoy
请发表您的ANDROIDVERSION类 –
使用@SerializedName解析,比你的领域应符合JSON的名字。将json字段名称放入注释中。 –