开发者社区> 问答> 正文

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,求告知是什么原因?

展开
收起
杨冬芳 2016-07-06 15:11:51 2346 0
1 条回答
写回答
取消 提交回答
  • IT从业

    List persons2 = gson.fromJson(jsonString, new TypeToken>(){}.getType()); 这种格式试试
    screenshot
    List list = gson.fromJson(jsonString, new TypeToken>() {}.getType());

    2019-07-17 19:52:10
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像