最佳答案
我想解析 JSON 数组并使用 gson。首先,我可以记录 JSON 输出,服务器正在清晰地响应客户端。
下面是我的 JSON 输出:
[
{
id : '1',
title: 'sample title',
....
},
{
id : '2',
title: 'sample title',
....
},
...
]
我尝试使用这种结构来解析.A 类,它依赖于单个 array
和 ArrayList
来解析所有 JSONArray。
public class PostEntity {
private ArrayList<Post> postList = new ArrayList<Post>();
public List<Post> getPostList() {
return postList;
}
public void setPostList(List<Post> postList) {
this.postList = (ArrayList<Post>)postList;
}
}
班级:
public class Post {
private String id;
private String title;
/* getters & setters */
}
当我尝试使用 gson 时,没有错误,没有警告,也没有日志:
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
PostEntity postEnt;
JSONObject jsonObj = new JSONObject(jsonOutput);
postEnt = gson.fromJson(jsonObj.toString(), PostEntity.class);
Log.d("postLog", postEnt.getPostList().get(0).getId());
怎么了,我怎么解决?