fast-json.jar的使用方法
fast-json.jar 解析json数据:一种json数据解析方式是这样的,点击这里下载jsonfast.jar+fastjsonAPI文档
[ { "id": 6378, "title": "test", "img": "http://image.jxvdy.com/2014/0929/5428d91c9e6dc8f78fd99_0.png", "score": 0, "description": "test", "time": 1411963174 }, { "id": 6142, "title": "微电影多角度拍摄技巧(三)", "img": "http://image.jxvdy.com/old/201409/24/11-54-15-17-1531.jpg", "score": 0, "description": "", "time": 1411530850 }, { "id": 6141, "title": "微电影多角度拍摄技巧(一)", "img": "http://image.jxvdy.com/old/201409/24/11-54-04-89-1531.jpg", "score": 0, "description": "", "time": 1411530835 }, { "id": 6140, "title": "微电影多角度拍摄技巧(二)", "img": "http://image.jxvdy.com/old/201409/24/11-49-54-18-1531.jpg", "score": 0, "description": "", "time": 1411530552 }, { "id": 4355, "title": "施比受,更有福", "img": "http://image.jxvdy.com/old/201409/24/11-46-06-65-3.jpg", "score": 0, "description": "一位老人用自己的一半时间去帮助他人,赠予帮助,收获快乐", "time": 1411530082 }, { "id": 4354, "title": "父子时光之旅", "img": "http://image.jxvdy.com/old/201409/24/11-35-13-81-3.jpg", "score": 0, "description": "当父亲老去,忙于生活的男人没有时间照顾体弱的父亲,于是,带上父亲上路吧,带他重走当年他走过无数遍的那段旅程", "time": 1411529699 } ]
对于这一种json数据,使用fastjson进行解析的时候,调用方法之前应该先写出其对应的bean.java(我想你已经做过了);上面的json数据对应的bean是这样的,
public class NewMoviesBean { private int id; private String title; private String img; private String score; private String description; private int time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } public NewMoviesBean(int id, String title, String img, String score, String description, int time) { super(); this.id = id; this.title = title; this.img = img; this.score = score; this.description = description; this.time = time; } public NewMoviesBean() { super(); } @Override public String toString() { return "NewMoviesBean [id=" + id + ", title=" + title + ", img=" + img + ", score=" + score + ", description=" + description + ", time=" + time + "]"; } }
那么对应的解析方法是这样的:
JSON.parseArray(json, NewMoviesBean.class);
为甚么回事这种解析方式呢?因为,分析整个json数据的格式我们能发现,最外层是中括号"[ ]",内侧是大括号"{ }";中括号说明整个json数据为一个数组类型,其中的大括号说明是数组中的元素;说明整个就是一个JSONArray,JSONArray中元素又是一个个的JSONObject。
另一种的解析方式:json数据是这样的,
{"type": [ "恐怖", "剧情" ]},
分析这种形式,大括号里面是小括号。也即是数组整体是通过键值对的形式呈现的。那么最外层就是一个JSONObject,KEY对应的就是JSONArray。应该这样:
JSONArray jsonArrayType = JSONObject.getJSONArray("type"); String[] type = new String[jsonArrayType.size()]; for (int j = 0; j < jsonArrayType.size(); j++) { type[j] = (String)jsonArrayType.get(j); }
这样就能够解析出想要的数据。
与上面类似的另一种解析:json数据是这样的:
{ "playurl": { "360P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGQ", "480P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGT", "720P": "http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_k6E0DCtzyZ8iv-pRF3GmewWOj-HQ_grNppGnnx_rRHb-bztNWAvzGZ" } }
这种形式,外层大括号里面是一个键KEY对应了另一个大括号元素,那么其最外层是一个JSONObject;内层KEY对应的也是一个JSONObject。
当然也可以先创建开一个bean:
public class MoviedefinitionBean { private String normalP; private String hightP; private String superP; public String getNormalP() { return normalP; } public void setNormalP(String normalP) { this.normalP = normalP; } public String getHightP() { return hightP; } public void setHightP(String hightP) { this.hightP = hightP; } public String getSuperP() { return superP; } public void setSuperP(String superP) { this.superP = superP; } public MoviedefinitionBean(String normalP, String hightP, String superP) { super(); this.normalP = normalP; this.hightP = hightP; this.superP = superP; } public MoviedefinitionBean() { super(); } @Override public String toString() { return "MoviedefinitionBean [normalP=" + normalP + ", hightP=" + hightP + ", superP=" + superP + "]"; } }
然后对此做出解析:
JSONObject jsonObjectDefination = jsonObject.getJSONObject("playurl"); String normalP = jsonObjectDefination.getString("360P"); String hightP = jsonObjectDefination.getString("480P"); String superP = jsonObjectDefination.getString("720P"); playurl = new MoviedefinitionBean(normalP, hightP, superP);
今天先写到这里|10-02-2014.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。