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

解析前的对象:
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个回答

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

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

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问