app设计中,很多倾向于使用json。
如果数据传递中,目录层次结构多,设计到的方面比较多了,json层级结构就比较复杂。
json解析有很多种方式,网上流传的fastjson是最快的方式。因此我自己做的糗事百科客户端中也使用了这个。这里记录下使用。
首先,看一段json数据。
{ "count": 30, "items": [ { "image": null, "published_at": 1415478930, "tag": "", "user": null, "image_size": null, "id": "93464687", "votes": { "down": -86, "up": 578 }, "created_at": 1415478930, "content": "说个朋友的事情吧!很长,耐心看完!简短割一下,纯手打。一个男性朋友,以下简称齐,他是在动车组工作的,28岁,身高1.8米,相貌端正,不抽烟不喝酒,不好名牌,月薪5000,人很不错,去年夏天相亲认识个女的,以下简称璐,27岁,身高也就1.6米,工作临时工,月薪1000,喜欢车(自己买不起,家里不给买),什么东西都喜欢好的。今年俩人研究结婚,我们之间比较熟络,这是背景。之前问她处没处过对象,璐说就处过一个,处了半年。问过她和齐有没有住一起,璐说,夜不归宿她爸爸会打死她,老公是警察,公安网上查的到任何东西,那天璐让老公帮忙查点东西,结果老公意外发现璐的开房记录多达20多条,亮点是分别和6个不同的男的!!!他俩都看糗百,还是逆了吧!!!希望顶高点让男的看到,擦亮眼睛,别花了50多万娶家个好几手货!这条过了再爆璐家里提的各种奇葩要求!", "state": "publish", "comments_count": 30, "allow_comment": true }, { "image": null, "published_at": 1415476799, "tag": "", "user": { "created_at": "0", "last_device": null, "role": "n", "last_visited_at": "0", "state": "active", "login": "王致和—逗妇乳", "id": "21544723", "icon": "20141016154050.jpg" }, "image_size": null, "id": "93461303", "votes": { "down": -41, "up": 456 }, "created_at": 1415476799, "content": "老妈:有对象了嘛? 我:没有呢 老妈:有了的话记得说啊,你那点生活费,不够的,别跟你爸似的,当年追我,天天邀我逛街,自己偷偷啃馒头,如果有了,我会给你财政拨款的,,, ,,,,, 顿时泪奔,这才是亲妈啊,,", "state": "publish", "comments_count": 4, "allow_comment": true },
可以看出: item下包含了votes ,user , image_size....算是比较负责的了。
其次,解析。我们简历ItemBean, UserBean ,VoteBean,ImageSize . 这里需要注意的是
被包含在ItemBean中的UserBean,和ImageSize 实例名需要和json中的一致。否则不能完全对应解析。
/** * 用户信息 */ private UserBean user; /** * 点赞信息 */ private VotesBean votes;
ItemBean
package com.qiubaiclient.model; import java.io.Serializable; /** * ItemBean JSON三级数据 * * @author xiangxm * */ public class ItemBean implements Serializable { private int blog_type; public int getBlog_type() { return blog_type; } public void setBlog_type(int blog_type) { this.blog_type = blog_type; } /** * 是否点击过 */ private boolean isClicked = false; public boolean isClicked() { return isClicked; } public void setClicked(boolean isClicked, int whoClicked) { this.isClicked = isClicked; if (null != votes) { votes.setWhoClicked(whoClicked); } } /** * */ private static final long serialVersionUID = 1L; /** * 文章附带的图片附件 */ private String image; /** * 发布时间 */ private String published_at; /** * 标志。暂时不知道是什么作用。 */ private String tag; /** * 文章对应的id */ private String id; /** * 文章创建时间。区别于published_at */ private String created_at; /** * 文章的内容 */ private String content; /** * 文章状态。糗百里面发布文章需要审核,估计这个是审核用的 */ private String state; /** * 评论数量 */ private int comments_count; /** * 是否允许评论 */ private boolean allow_comment; /** * 用户信息 */ private UserBean user; /** * 点赞信息 */ private VotesBean votes; private ImageSize image_size; public ImageSize getImage_size() { return image_size; } public void setImage_size(ImageSize image_size) { this.image_size = image_size; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getPublished_at() { return published_at; } public void setPublished_at(String published_at) { this.published_at = published_at; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCreated_at() { return created_at; } public void setCreated_at(String created_at) { this.created_at = created_at; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getState() { return state; } public void setState(String state) { this.state = state; } public int getComments_count() { return comments_count; } public void setComments_count(int comments_count) { this.comments_count = comments_count; } public boolean isAllow_comment() { return allow_comment; } public void setAllow_comment(boolean allow_comment) { this.allow_comment = allow_comment; } public UserBean getUser() { return user; } public void setUser(UserBean user) { this.user = user; } public VotesBean getVotes() { return votes; } public void setVotes(VotesBean votes) { this.votes = votes; } @Override public String toString() { return "ItemBean [blog_type=" + blog_type + ", isClicked=" + isClicked + ", image=" + image + ", published_at=" + published_at + ", tag=" + tag + ", id=" + id + ", created_at=" + created_at + ", content=" + content + ", state=" + state + ", comments_count=" + comments_count + ", allow_comment=" + allow_comment + ", user=" + user + ", votes=" + votes + ", image_size=" + image_size + "]"; } }
UserBean
package com.qiubaiclient.model; import java.io.Serializable; /** * JSON五级数据 用户信息 * * @author xiangxm * */ public class UserBean implements Serializable{ /** * */ private static final long serialVersionUID = 1L; /** * 状态标志位,暂时不知道什么用 */ private String created_at; /** * 用户使用设备发表 */ private String last_device; /** * 用户角色 */ private String role; /** * 最后访问 */ private String last_visited_at; /** * 用户活跃状态 */ private String state; /** * 登录用户名 */ private String login; /** * 用户id */ private String id; /** * 用户头像icon名称 */ private String icon; public String getCreated_at() { return created_at; } public void setCreated_at(String created_at) { this.created_at = created_at; } public String getLast_device() { return last_device; } public void setLast_device(String last_device) { this.last_device = last_device; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getLast_visited_at() { return last_visited_at; } public void setLast_visited_at(String last_visited_at) { this.last_visited_at = last_visited_at; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } @Override public String toString() { return "UserBean [created_at=" + created_at + ", last_device=" + last_device + ", role=" + role + ", last_visited_at=" + last_visited_at + ", state=" + state + ", login=" + login + ", id=" + id + ", icon=" + icon + "]"; } }
VotesBean
package com.qiubaiclient.model; import java.io.Serializable; /** * 点赞与不点赞的数据 * * @author xiangxm * */ public class VotesBean implements Serializable{ /** * */ private static final long serialVersionUID = 1L; /** * 不点赞 */ private int down; /** * 点赞 */ private int up; public int getDown() { return down; } public void setDown(int down) { this.down = down; } public int getUp() { return up; } public void setUp(int up) { this.up = up; } @Override public String toString() { return "VotesBean [down=" + down + ", up=" + up + "]"; } /** * 身份识别,看是up被点击了还是其他的被点击了。 */ private int whoClicked = 0 ; public int getWhoClicked() { return whoClicked; } public void setWhoClicked(int whoClicked) { this.whoClicked = whoClicked; } public static final int UP_CLICKED = 0 ; public static final int DOWN_CLICKED = 1 ; public static final int SHARE_CLICKED = 2 ; }
ImageSize
package com.qiubaiclient.model; import java.io.Serializable; /** * 照片尺寸 * * @author xiangxm * */ public class ImageSize implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private int[] s; private int[] m; public int[] getS() { return s; } public void setS(int[] s) { this.s = s; } public int[] getM() { return m; } public void setM(int[] m) { this.m = m; } }
ArticleBean
package com.qiubaiclient.model; import java.util.ArrayList; import java.util.List; /** * * 使用fastjson解析 * * @author xiangxm * */ public class ArticleBean { private int count; private long total; private int page; /** * items所对应的数组 */ private List<ItemBean> items = new ArrayList<ItemBean>(); public List<ItemBean> getItems() { return items; } public void setItems(List<ItemBean> items) { this.items = items; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } @Override public String toString() { return "ArticleBean [count=" + count + ", total=" + total + ", page=" + page + ", items=" + items + "]"; } }
解析:
List<ItemBean> tempList = JSON.parseObject(responseStr, ArticleBean.class).getItems();