JSON数据之使用Fastjson进行解析(一)
2017-05-07 21:21
459 查看
据说FastJson是目前最快的解析Json数据的库,而且是国人开发出来的开源库。顶一下,付上官方网址:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=2424946
要使用Fastjson,首先需要下载相对应的jar文件,在官网即可下载。
附上初学的第一个例子,多多指教:
AllBean的Bean类:
UserBean的Bean类:
解析类JsonBean:
最后,附上程序代码:FastJsonTest.zip
from: http://www.cnblogs.com/lee0oo0/archive/2013/05/08/3066371.html
要使用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
相关文章推荐
- 解析Json数据:使用FastJson进行解析json数据<三>
- JSON数据之使用Fastjson进行解析(一)
- Android中Json数据解析(二)--使用Gson、Jackson和FastJson解析Json数据
- 使用jQuery解析JSON数据(jquery在前台对从后台获取到的json数据进行解析,并进行显示)
- 使用fastJSON解析HashMap中的数据
- fastjson进行复杂微博数据解析
- JavaScript使用json2.js对json数据进行解析
- Andorid中使用Gson和Fast-json解析库解析JSON数据---第三方库学习笔记(二)
- FastJSON使用心得——如何解析带中文或者数字key的数据
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- 使用FastJson解析Json数据
- Android使用GSON和FASTJSON解析服务器返回的JSON数据
- 如何使用fastJson来解析JSON格式数据和生成JSON格式数据
- Android访问网络:服务端返回XML或JSON格式数据,Android 进行解析并使用ListView显示
- 使用FastJson解析Json数据
- 如何使用fastJson来解析JSON格式数据和生成JSON格式数据
- Android网络开发中如何使用JSON进行网络通信---Android_JSON数据通讯方法解析