fastjsonfastjson和gson的区别
fastjson 时间:2021-07-30 阅读:(
)
怎么用fastJson解析这个比较复杂的json,请帮忙
最简单就是 Object t=JSON.parse("jsonStr");
如果你要生成模型就
TModel tModel=new TModel();
tModel=JSON.parseObject("jsonstr", TModel.class);复杂json怎么使用fastjson一次性提取出来
JSON数据之使用Fastjson进行解析(一)
据说FastJson是目前最快的解析Json数据的库,而且是国人开发出来的开源库。
顶一下,付上官方网址:h//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类:
复制代码
.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类:
复制代码
.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:
复制代码
.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;
.alibaba.fastjson.JSON;
.alibaba.fastjson.JSONArray;
.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 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;
}
}fastjson和gson的区别
展开全部
gson和fastjson我都在项目使用过了,相比较而言,gson对字段的处理更细腻一些,有版本号的概念,相对更灵活,值得注意是,gson直接入侵字段,则不是setget方法;fastjson速度更快,但字段的处理不够灵活,特别是处理同一个Javabean的时候,
每年的7月的最后一个周五是全球性质的“系统管理员日”,据说是为了感谢系统管理员的辛苦工作....friendhosting决定从现在开始一直到9月8日对其全球9个数据中心的VPS进行4.5折(优惠55%)大促销。所有VPS基于KVM虚拟,给100M带宽,不限制流量,允许自定义上传ISO...官方网站:https://friendhosting.net比特币、信用卡、PayPal、支付宝、微信、we...
日前,国内知名主机服务商阿里云与国外资深服务器面板Plesk强强联合,推出 阿里云域名注册与备案、服务器ECS购买与登录使用 前言云服务器(Elastic 只需要确定cpu内存与带宽基本上就可以了,对于新手用户来说,我们在购买阿里云服务申请服务器与域名许多云服务商的云服务器配置是弹性的 三周学会小程序第三讲:服务 不过这个国外服务器有点慢,可以考虑国内的ngrokcc。 ngrokcc...
racknerd从成立到现在发展是相当迅速,用最低的价格霸占了大部分低端便宜vps市场,虽然VPS价格便宜,但是VPS的质量和服务一点儿都不拉跨,服务器稳定、性能给力,尤其是售后方面时间短技术解决能力强,估计这也是racknerd这个品牌能如此成功的原因吧! 官方网站:https://www.racknerd.com 多种加密数字货币、信用卡、PayPal、支付宝、银联、webmoney,可...
fastjson为你推荐
word2003公式编辑器word2003里的公式编辑器怎么用啊?office软件包不属于MIcrosoft Office软件包的软件是 ? A Mailwin7无线局域网如何用Win7搭建无线局域网?怎样恢复系统怎么还原系统免流量是什么意思4g手机浏览器免流量是什么意思excel2003官方Microsoft Office Excel 2003怎么下载windowsphone手机Windows Phone 手机有哪些iphone12或支持北斗导航苹果12屏幕设置淘气鸟乌儿很淘气,飞来飞去,蹦蹦跳跳,请你用“一会儿…一会儿…一会儿…”写写鸟儿?呼叫中心搭建如何建立适合自己的呼叫中心
最好的虚拟主机 免费二级域名 网站域名备案 免费二级域名申请 sharktech oneasiahost 256m内存 美国主机网 鲨鱼机 国内永久免费云服务器 香港cdn godaddy支付宝 国外免费空间 刀片服务器是什么 idc资讯 asp免费空间申请 免费测手机号 常州联通宽带 in域名 香港亚马逊 更多