【发布时间】:2012-01-12 08:45:05
【问题描述】:
我想解析 JSON 数组并使用 gson。首先,我可以记录 JSON 输出,服务器清楚地响应客户端。
这是我的 JSON 输出:
[
{
id : '1',
title: 'sample title',
....
},
{
id : '2',
title: 'sample title',
....
},
...
]
我尝试了这种结构进行解析。一个类,它依赖于所有 JSONArray 的单个 array 和 ArrayList。
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());
怎么了,怎么解决?
【问题讨论】:
标签: java android arrays json gson