1
Amarok OP 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 )的时候就报空指针... |
2
Amarok OP getPostings().size()就报空指针...T.T
|
3
hinkal 2016-12-21 11:33:19 +08:00 via Android
你这个代码没贴全, PostingExample 的类结构呢
|
4
xuhaoyangx 2016-12-21 11:33:20 +08:00
只给代码不给错误信息,而且你这个代码也不全,只说空指针,可以
|
5
Amarok OP 还没发完就被老板叫过去了....回来补上代码....
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; } |
6
Amarok OP 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" } ] } |
8
CFO 2016-12-21 11:39:18 +08:00 via Android
实体类还是用 GsonFormat 生成吧 靠谱
|
9
Amarok OP @xuhaoyangx 太着急了刚刚,已经贴上了.....谢谢~
|
10
sjn9588 2016-12-21 11:40:15 +08:00
postingExample.getPostings()返回的 Postings 是 null 吧?
|
11
Ouyangan 2016-12-21 11:40:18 +08:00
我想说你为什么不把反序列化出来的对象打印出来呢?
|
12
shakespaces 2016-12-21 11:43:28 +08:00
你这个类方法没看到和 content 相关的东西啊
|
13
sorra 2016-12-21 11:44:49 +08:00
一个叫 Posting ,一个叫 Postings ,不匹配啊
|
14
xuhaoyangx 2016-12-21 11:45:06 +08:00
我怎么感觉的你的 json 有问题呢。。
|
15
xuhaoyangx 2016-12-21 11:46:30 +08:00 2
private List<PostingDetail> Postings
但是你的 json 那个数组名字是 Posting |
18
Amarok OP @xuhaoyangx 啊啊啊啊啊我瞎了.....这么低级的问题,捂脸......
|
19
pubby 2016-12-21 12:32:57 +08:00
|
20
wanttofly 2016-12-21 13:30:46 +08:00
话说不用 GsonFormat 吗?应该不会出现变量名这个问题了啊。。
|