Gson 报空指针...死活搞不明白怎么回事,求指点~~

2016-12-21 11:26:01 +08:00
 Amarok
9233 次点击
所在节点    Android
20 条回复
Amarok
2016-12-21 11:27:07 +08:00
String jsonText = jsonStringTool.Optimization(s);
Log.e(TAG, jsonText);


Gson detailGson = new Gson();

PostingExample postingExample = detailGson.fromJson(jsonText,PostingExample.class);
String id = postingExample.getPostings().get(1).getID();
String pID = postingExample.getPostings().get(1).getPid();

String Contents = postingExample.getPostings().get(1).getContents();
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("pID", pID);


map.put("Contents", Contents);

log 出来的 json 字符串没问题...到操作 get ( 1 )的时候就报空指针...
Amarok
2016-12-21 11:31:56 +08:00
getPostings().size()就报空指针...T.T
hinkal
2016-12-21 11:33:19 +08:00
你这个代码没贴全, PostingExample 的类结构呢
xuhaoyangx
2016-12-21 11:33:20 +08:00
只给代码不给错误信息,而且你这个代码也不全,只说空指针,可以
Amarok
2016-12-21 11:36:38 +08:00
还没发完就被老板叫过去了....回来补上代码....

PostingExample.class
public class PostingExample {
private List<PostingDetail> Postings;

public List<PostingDetail> getPostings() {
return Postings;
}

public void setPostings(List<PostingDetail> postings) {
Postings = postings;
}

}


PostingDetail .class
public class PostingDetail {
private String FocusName;
private String status;
private String sportsName;
private String ptype;
private String UserName;
private String ID;
private String postType;
private String baseTitle;
private String fixedTitle;
private String sportstypeName;
private String collection;
private String sendTime;
private String pid;
private String titleimg;
private String contents;

public String getID() {
return ID;
}

public void setID(String ID) {
this.ID = ID;
}

public String getBaseTitle() {
return baseTitle;
}

public void setBaseTitle(String baseTitle) {
this.baseTitle = baseTitle;
}

public String getFixedTitle() {
return fixedTitle;
}

public void setFixedTitle(String fixedTitle) {
this.fixedTitle = fixedTitle;
}

public String getCollection() {
return collection;
}

public void setCollection(String collection) {
this.collection = collection;
}

public String getSendTime() {
return sendTime;
}

public void setSendTime(String sendTime) {
this.sendTime = sendTime;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getPid() {
return pid;
}

public void setPid(String pid) {
this.pid = pid;
}

public String getTitleimg() {
return titleimg;
}

public void setTitleimg(String titleimg) {
this.titleimg = titleimg;
}

public String getFocusName() {
return FocusName;
}

public void setFocusName(String focusName) {
FocusName = focusName;
}

public String getSportstypeName() {
return sportstypeName;
}

public void setSportstypeName(String sportstypeName) {
this.sportstypeName = sportstypeName;
}

public String getSportsName() {
return sportsName;
}

public void setSportsName(String sportsName) {
this.sportsName = sportsName;
}

public String getPtype() {
return ptype;
}

public void setPtype(String ptype) {
this.ptype = ptype;
}

public String getUserName() {
return UserName;
}
Amarok
2016-12-21 11:38:04 +08:00
jsonText :
{
"Posting": [
{
"UserName": "小袁",
"FocusName": "衰神",
"status": "未审核",
"sportsName": "网球",
"ptype": "原创",
"ID": "11",
"sportstypeName": "竞技球类",
"baseTitle": "小米手环 2 开箱及与小米手环 1 对比",
"fixedTitle": "小米哈哈 666",
"seen": "900",
"collection": "170",
"sendTime": "08 27 2016 11:11AM",
"pid": "20160827111111",
"titleimg": "http://imageqiniu.xxxxxbbs.com/FrtK2K9JqwoE6xhYWRPFv9tIZym4?watermark/1/image/aHR0cDovLzd4c2N3NS5jb20wLnowLmdsYi5xaW5pdWNkbi5jb20vd2F0ZXJtYXJrLnBuZw==/gravity/South/dy/5",
"Contents": "aaa"
}
]
}
Amarok
2016-12-21 11:39:06 +08:00
@hinkal 贴上了....谢谢~
CFO
2016-12-21 11:39:18 +08:00
实体类还是用 GsonFormat 生成吧 靠谱
Amarok
2016-12-21 11:40:00 +08:00
@xuhaoyangx 太着急了刚刚,已经贴上了.....谢谢~
sjn9588
2016-12-21 11:40:15 +08:00
postingExample.getPostings()返回的 Postings 是 null 吧?
Ouyangan
2016-12-21 11:40:18 +08:00
我想说你为什么不把反序列化出来的对象打印出来呢?
shakespaces
2016-12-21 11:43:28 +08:00
你这个类方法没看到和 content 相关的东西啊
sorra
2016-12-21 11:44:49 +08:00
一个叫 Posting ,一个叫 Postings ,不匹配啊
xuhaoyangx
2016-12-21 11:45:06 +08:00
我怎么感觉的你的 json 有问题呢。。
xuhaoyangx
2016-12-21 11:46:30 +08:00
private List<PostingDetail> Postings
但是你的 json 那个数组名字是 Posting
Amarok
2016-12-21 11:47:08 +08:00
@sjn9588 对...是 null
sjn9588
2016-12-21 11:48:57 +08:00
@Amarok 15 楼正解
Amarok
2016-12-21 11:49:41 +08:00
@xuhaoyangx 啊啊啊啊啊我瞎了.....这么低级的问题,捂脸......
pubby
2016-12-21 12:32:57 +08:00
@SerializedName("Posting")
private List<PostingDetail> Postings;

名字不一致没关系,上面加个注解也能解决
wanttofly
2016-12-21 13:30:46 +08:00
话说不用 GsonFormat 吗?应该不会出现变量名这个问题了啊。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/329102

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX