JSON數據之使用Fastjson進行解析(一)


據說FastJson是目前最快的解析Json數據的庫,而且是國人開發出來的開源庫。頂一下,付上官方網址:http://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;  
    }  
}


最后,附上程序代碼:FastJsonTest.zip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM