Angry_Ants 2015-11-30 14:03 采纳率: 20%
浏览 1647
已结题

Gson解析jsonString 出现问题,急!急!急!

解析前的对象:
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,求告知是什么原因?

  • 写回答

3条回答

  • 晓呆同学 2015-11-30 14:44
    关注

    List list = gson.fromJson(jsonString, new TypeToken>() {}.getType());

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题