fast-json.jar的使用方法

來源:互聯網
上載者:User

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();}@Overridepublic 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();}@Overridepublic 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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.