JSON数据之使用Fastjson进行解析(一)
2013-05-08 11:11
666 查看
据说FastJson是目前最快的解析Json数据的库,而且是国人开发出来的开源库。顶一下,付上官方网址:http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=2424946
要使用Fastjson,首先需要下载相对应的jar文件,在官网即可下载。
附上初学的第一个例子,多多指教:
AllBean的Bean类:
UserBean的Bean类:
解析类JsonBean:
最后,附上程序代码:FastJsonTest.zip
要使用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
相关文章推荐
- 解析Json数据:使用FastJson进行解析json数据<三>
- JSON数据之使用Fastjson进行解析(一)
- (二)初识springboot之使用fastjson解析数据(解决乱码及简单配置)
- Android开发中使用fastJSON将json格式的数据快速解析生成实体类并附上实体类生成器
- FastJSON使用心得——如何解析带中文或者数字key的数据
- 如何使用fastJson来解析JSON格式数据和生成JSON格式数据
- Spring Boot使用FastJson解析JSON数据
- Spring Boot使用FastJson解析JSON数据的方法
- 使用FastJson解析Json数据
- 使用FastJson解析Json数据
- JAVA-使用FastJson解析复杂JSON数据
- 【Spring Boot】完美使用FastJson解析JSON数据
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Andorid中使用Gson和Fast-json解析库解析JSON数据---第三方库学习笔记(二)
- Android中Json数据解析(二)--使用Gson、Jackson和FastJson解析Json数据
- fastjson进行复杂微博数据解析
- Spring Boot使用FastJson解析JSON数据
- c# 使用Newtonsoft.Json 对数据进行打包与解析
- Android使用GSON和FASTJSON解析服务器返回的JSON数据
- Android使用JSON进行数据解析