解析前的对象:
public class BBSInformation {
private String BBSTitle = null;
private String BBSAuthor = null;
private String BBSContent = null;
private String BBSTime = null;
private String BBSKeywords = null;
private int BBSAgree;
private int BBSDisagree;
private int BBSCommentNum;
private String BBSUUID = null;
public String getBBSTitle() {
return BBSTitle;
}
public void setBBSTitle(String bBSTitle) {
BBSTitle = bBSTitle;
}
public String getBBSAuthor() {
return BBSAuthor;
}
public void setBBSAuthor(String bBSAuthor) {
BBSAuthor = bBSAuthor;
}
public String getBBSContent() {
return BBSContent;
}
public void setBBSContent(String bBSContent) {
BBSContent = bBSContent;
}
public String getBBSTime() {
return BBSTime;
}
public void setBBSTime(String bBSTime) {
BBSTime = bBSTime;
}
public String getBBSKeywords() {
return BBSKeywords;
}
public void setBBSKeywords(String bBSKeywords) {
BBSKeywords = bBSKeywords;
}
public int getBBSAgree() {
return BBSAgree;
}
public void setBBSAgree(int bBSAgree) {
BBSAgree = bBSAgree;
}
public int getBBSDisagree() {
return BBSDisagree;
}
public void setBBSDisagree(int bBSDisagree) {
BBSDisagree = bBSDisagree;
}
public int getBBSCommentNum() {
return BBSCommentNum;
}
public void setBBSCommentNum(int bBSCommentNum) {
BBSCommentNum = bBSCommentNum;
}
public String getBBSUUID() {
return BBSUUID;
}
public void setBBSUUID(String bBSUUID) {
BBSUUID = bBSUUID;
}
}
生成的代码:
try {
//数据库查询
ResultSet rs = dd.query(sql, count);
while(rs.next()) {
BBSInformation bbsInfo = new BBSInformation();
bbsInfo.setBBSUUID(rs.getString("article_uuid"));
bbsInfo.setBBSAuthor("article_author");
bbsInfo.setBBSTime(rs.getDate("article_date").toString());
bbsInfo.setBBSTitle(rs.getString("article_title"));
bbsInfo.setBBSContent(rs.getString("article_content"));
bbsInfo.setBBSKeywords(rs.getString("article_key"));
bbsInfo.setBBSAgree(rs.getInt("article_agree"));
bbsInfo.setBBSDisagree(rs.getInt("article_disagree"));
bbsInfo.setBBSCommentNum(rs.getInt("article_comment"));
bbsInformations.add(bbsInfo);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(bbsInformations.get(0).getBBSAuthor());
/*GetBBSIformation getBBSIformation = new GetBBSIformation();
getBBSIformation.setList(bbsInformations);*/
Gson gson = new Gson();
String bbsInfoJson = new WriteJson().getJsonData(bbsInformations);
System.out.println(bbsInfoJson);
response.setContentType("text/xml; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(bbsInfoJson);
out.flush();
out.close();
生成的jsonString:
[{"BBSAuthor":"article_author","BBSContent":"我是一只小小小鸟","BBSTime":"1994-06-17","BBSKeywords":"android,java,c语言","BBSAgree":5,"BBSDisagree":2,"BBSCommentNum":3,"BBSUUID":"254ebfed-90d2-464f-a76b-a55aed6e8fb7"}]
解析的源代码:
Type listType = new TypeToken>(){}.getType();
Gson gson=new Gson();
List list=gson.fromJson(jsondata, listType);
生成json和解析json用的是同一个对象BBSInformation,,弄了好久啊,
就是到这里,没有返回list,求告知是什么原因?
List persons2 = gson.fromJson(jsonString, new TypeToken>(){}.getType()); 这种格式试试
List list = gson.fromJson(jsonString, new TypeToken>() {}.getType());
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。