据说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
from: http://www.cnblogs.com/lee0oo0/archive/2013/05/08/3066371.html