1. 程式人生 > >fastjson使用方法簡析

fastjson使用方法簡析

JSON資料之使用Fastjson進行解析(一)

據說FastJson是目前最快的解析Json資料的庫,而且是國人開發出來的開源庫。頂一下,付上官方網址:h/code.alibabatech.com/wiki/pages/viewpage.action?pageId=2424946

要使用Fastjson,首先需要下載相對應的jar檔案,在官網即可下載。
附上初學的第一個例子,多多指教:

複製程式碼
{
    "statuses":[
        {
         "id": 912345678901,
         "text": "How do I stream JSON in Java?",
         "geo": null,
         "user": {
        "name": "json_newb",
        "followers_count": 41
              }
          },
          
        {
         "id": 777777777888,
         "text": "dfngsdnglnsldfnsl",
         "geo": null,
         "user": {
        "name": "dsfgpd",
        "followers_count": 24
              }
          }
     ]
}
複製程式碼

AllBean的Bean類:

複製程式碼
package com.lee.JsonToBean;

public class AllBean {
    private long id;
    private String text;
    private String geo;
    private UserBean userBean;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getGeo() {
        return geo;
    }
    public void setGeo(String geo) {
        this.geo = geo;
    }
    public UserBean getUserBean() {
        return userBean;
    }
    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }
    
}
複製程式碼

UserBean的Bean類:

複製程式碼
package com.lee.JsonToBean;

public class UserBean {
    private String name;
    private int followers_count;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getFollowers_count() {
        return followers_count;
    }
    public void setFollowers_count(int followers_count) {
        this.followers_count = followers_count;
    }
}
複製程式碼

解析類JsonBean:

複製程式碼
package com.lee.JsonToBean;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * {
    "statuses":[
        {
         "id": 912345678901,
         "text": "How do I stream JSON in Java?",
         "geo": null,
         "user": {
        "name": "json_newb",
        "followers_count": 41
              }
          },
          
        {
         "id": 777777777888,
         "text": "dfngsdnglnsldfnsl",
         "geo": null,
         "user": {
        "name": "dsfgpd",
        "followers_count": 24
              }
          }
     ]
} 
 * */
public class JsonBean {
    RTFEditorKit rtf;
    DefaultStyledDocument dsd;
    String text;
    public static void main(String[] args) {
        JsonBean bean = new JsonBean();
        // 把字串轉為Json物件,這是因為我的json資料首先是json物件
        JSONObject jobj = JSON.parseObject(bean.readRtf(new File("json.rtf")));
        // 然後是jsonArray,可以根據我的json資料知道
        JSONArray arr = jobj.getJSONArray("statuses");
        // 根據Bean類的到每一個json陣列的項
        List<AllBean> listBeans = JSON.parseArray(arr.toString(), AllBean.class);
        // 遍歷
        for(AllBean bean_ : listBeans){
            // 我這個demo的json資料獲得第一層的資料
            System.out.println(bean_.getText());
            System.out.println(bean_.getId());
            // 我這個demo的json資料獲得第二層的資料
            System.out.println(bean_.getUserBean().getFollowers_count());
        }
    }
    
    // 因為我把json資料放進rtf檔案,這是讀取rtf檔案的json資料,轉化為字串
    public String readRtf(File in) {  
        rtf=new RTFEditorKit();  
        dsd=new DefaultStyledDocument();  
        try {  
            rtf.read(new FileInputStream(in), dsd, 0);  
            text = new String(dsd.getText(0, dsd.getLength()));  
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (BadLocationException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }
        return text;  
    }  
}