fastjson详解
Posted 吴声子夜歌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fastjson详解相关的知识,希望对你有一定的参考价值。
一、fastjson介绍
在前后端数据传输交互中,经常会遇到字符串(String)与json,XML等格式相互转换与解析,其中json以跨语言,跨前后端的优点在开发中被频繁使用,基本上可以说是标准的数据交换格式。fastjson 是一个java语言编写的高性能且功能完善的JSON库,它采用一种“假定有序快速匹配”的算法,把JSON Parse 的性能提升到了极致。它的接口简单易用,已经被广泛使用在缓存序列化,协议交互,Web输出等各种应用场景中。
FastJson是啊里巴巴的的开源库,用于对JSON格式的数据进行解析和打包。
特点如下:
- 能够支持将java bean序列化成JSON字符串,也能够将JSON字符串反序列化成Java bean。
- 顾名思义,fastjson操作 JSON的速度是非常快的。
- 无其他包的依赖。
- 使用比较方便。
二、fastjson使用
maven依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<!--(起码1.2.48以上)因为这个版本一下存在漏洞-->
<version>版本根据自己需要</version>
</dependency>
三、fastjson常用API
fastjson API 入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。
四、示例
public class User
private String username;
private String password;
public User()
public User(String username, String password)
this.username = username;
this.password = password;
public String getUsername()
return username;
public void setUsername(String username)
this.username = username;
public String getPassword()
return password;
public void setPassword(String password)
this.password = password;
@Override
public String toString()
return "User" +
"username='" + username + '\\'' +
", password='" + password + '\\'' +
'';
public class UserGroup
private String name;
private List<User> users = new ArrayList<User>();
public UserGroup()
public UserGroup(String name, List<User> users)
this.name = name;
this.users = users;
public String getName()
return name;
public void setName(String name)
this.name = name;
public List<User> getUsers()
return users;
public void setUsers(List<User> users)
this.users = users;
@Override
public String toString()
return "UserGroup" +
"name='" + name + '\\'' +
", users=" + users +
'';
1. java类转换为json字符串:
/**
* java对象转换为json字符串
*/
@Test
public void objToJson()
//简单对象转换
User user = new User("root", "123456");
//调用toJSONString()
String userJson = JSON.toJSONString(user);
System.out.println("java类转换为json串:" + userJson);
//集合转json串
User user1 = new User("zhangsan", "123456");
User user2 = new User("lisi", "000");
List<User> users = new ArrayList<User>();
users.add(user1);
users.add(user2);
//调用toJSONString()
String usersjson = JSON.toJSONString(users);
System.out.println("集合转json串:" + usersjson);
//复杂java类转换对象
UserGroup userGroup = new UserGroup("userGroup", users);
//调用toJSONString()
String userGroupJson = JSON.toJSONString(userGroup);
System.out.println("复杂java类转换json串:" + userGroupJson);
java类转换为json串:"password":"123456","username":"root"
集合转json串:["password":"123456","username":"zhangsan","password":"000","username":"lisi"]
复杂java类转换json串:"name":"userGroup","users":["password":"123456","username":"zhangsan","password":"000","username":"lisi"]
2. json字符串转为java类:
/**
* json字符串转为java类
* 注:字符串中使用双引号需要转义 (" --> \\"),这里使用的是单引号,易读性会好很多。
* json串以“”包裹,转换为java类时,使用:parseObject();
* json串以“[]”包裹,转换为java类时,使用:parseArray();
*/
@Test
public void jsonToObj()
/* json字符串转简单java对象
* 字符串:"password":"123456","username":"dmego"
*/
String jsonStr1 = "'password':'123456','username':'ggf'";
// 调用parseObject()
User user = JSON.parseObject(jsonStr1, User.class);
System.out.println("json字符串转简单java对象:"+user.toString());
/*
* json字符串转List<Object>对象
* 字符串:["password":"123123","username":"zhangsan",
* "password":"321321","username":"lisi"]
*/
String jsonStr2 = "['password':'123123','username':'zhangsan','password':'321321','username':'lisi']";
// 调用parseArray()将字符串转为集合
List<User> users = JSON.parseArray(jsonStr2, User.class);
System.out.println("json字符串转List<Object>对象:"+users.toString());
/*json字符串转复杂java对象
* 字符串:"name":"userGroup","users":["password":"123123","username":"zhangsan","password":"321321","username":"lisi"]
* */
String jsonStr3 = "'name':'userGroup','users':['password':'123123','username':'zhangsan','password':'321321','username':'lisi']";
UserGroup userGroup = JSON.parseObject(jsonStr3, UserGroup.class);
System.out.println("json字符串转复杂java对象:"+userGroup);
json字符串转简单java对象:Userusername='ggf', password='123456'
json字符串转List<Object>对象:[Userusername='zhangsan', password='123123', Userusername='lisi', password='321321']
json字符串转复杂java对象:UserGroupname='userGroup', users=[Userusername='zhangsan', password='123123', Userusername='lisi', password='321321']
五、实际应用
1. 对复杂的json串转为java类:
首先有这么一个json字符串,这是一个羊肉汤的菜谱,数据来源于《聚合数据》
"resultcode":"200",
"reason":"Success",
"result":
"data":[
"id":"6269",
"title":"羊肉汤",
"tags":"增强抵抗力;煮;家常菜;汤;鲁菜",
"imtro":"邹城人有喝羊汤的习惯,春夏秋冬羊汤馆总断不了食客,春秋天气候干燥要喝,夏天入伏要喝“伏羊汤”,阴冷的冬季尤其要喝碗羊汤才够温暖。以至于邀友喝羊汤成为了礼仪;“二哥,晚上咱们喝羊汤去”。邹城的羊汤铺遍地开花,以至于单县羊汤、滕州羊汤在邹城都没有了用武之地。我们这里的羊汤做法是最纯的,基本不放煮肉的香料,就用羊骨和羊肉煮成,“肉嫩汤浓”是其特色。 煮羊汤要先煮羊骨,把羊骨斩成大段焯水后放一点羊板油用细火煮,煮到汤白味浓时放入羊肉。羊肉煮到用筷子能轻松插穿时就要捞出,久煮的话羊肉过烂就失去了软嫩的口感。 碗里放入葱花或蒜粒,调入精盐,放入切的薄薄的羊肉片。把烧的滚开的羊汤盛到碗里,洒上香菜,再挖上一匙子香辣的用羊油泼成的辣椒油,一个字“香”!",
"ingredients":"山羊肉,500g;羊骨,1000g",
"burden":"生姜,适量;精盐,适量;香菜,适量;大葱,适量;辣椒油,适量;羊板油,适量",
"albums":[
"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/t\\/7\\/6269_379835.jpg"
],
"steps":[
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_95d65e77b58a1b6b.jpg",
"step":"1.羊脊骨洗净用刀斩成段。"
,
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_a8136c10401a1643.jpg",
"step":"2.煮锅里倒入清水,放入羊脊骨,羊肉煮开后捞出。"
,
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_c7b1c9fc85ddc6de.jpg",
"step":"3.煮锅里倒入开水,放入羊脊骨生姜块大火煮开后改小火。"
,
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_2b284dc30b4f0875.jpg",
"step":"4.小火煮40分钟,煮至汤色发白。"
,
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_c7ade6439eb2db5a.jpg",
"step":"5.放入羊肉,加入适量的羊板油小火煮30分钟。"
,
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_579748e3b0f15963.jpg",
"step":"6.捞出煮好的羊肉,晾凉后切薄片。"
,
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_1550e6f127aa1077.jpg",
"step":"7.碗里放入葱花,调入精盐。"
,
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_a2c965d77b96da70.jpg",
"step":"8.放入羊肉片,把滚开的羊汤倒入碗里洒上香菜末。"
,
"img":"http:\\/\\/juheimg.oss-cn-hangzhou.aliyuncs.com\\/cookbook\\/s\\/63\\/6269_eea9b807d1dc5995.jpg",
"step":"9.可以根据喜好调入陈醋放入蒜粒,最后调入辣椒油即可。"
]
],
"totalNum":"9",
"pn":0,
"rn":"1"
,
"error_code":0
要想解析这种复杂的字符串,把它转换成java类的话,首先得先定义好与之相符的java POJO 对象,从上面的json字符串组成来看,我们可以拆分出来四个bean:
最外层的响应:ResponseData
返回结果:ResultBean
数据:DataBean
做菜步骤:StepsBean
将拿到的json字符串数据,用GsonFormat工具来生成java类。
GsonFormat工具的使用可参考该文章:https://www.cnblogs.com/1024zy/p/6370305.html
public class ResponseData
/**
* resultcode : 200
* reason : Success
* result : "data":["id":"6269","title":"羊肉汤","tags":"增强抵抗力;煮;家常菜;汤;鲁菜","imtro":"邹城人有喝羊汤的习惯,春夏秋冬羊汤馆总断不了食客,春秋天气候干燥要喝,夏天入伏要喝\\u201c伏羊汤\\u201d,阴冷的冬季尤其要喝碗羊汤才够温暖。以至于邀友喝羊汤成为了礼仪;\\u201c二哥,晚上咱们喝羊汤去\\u201d。邹城的羊汤铺遍地开花,以至于单县羊汤、滕州羊汤在邹城都没有了用武之地。我们这里的羊汤做法是最纯的,基本不放煮肉的香料,就用羊骨和羊肉煮成,\\u201c肉嫩汤浓\\u201d是其特色。 煮羊汤要先煮羊骨,把羊骨斩成大段焯水后放一点羊板油用细火煮,煮到汤白味浓时放入羊肉。羊肉煮到用筷子能轻松插穿时就要捞出,久煮的话羊肉过烂就失去了软嫩的口感。 碗里放入葱花或蒜粒,调入精盐,放入切的薄薄的羊肉片。把烧的滚开的羊汤盛到碗里,洒上香菜,再挖上一匙子香辣的用羊油泼成的辣椒油,一个字\\u201c香\\u201d!","ingredients":"山羊肉,500g;羊骨,1000g","burden":"生姜,适量;精盐,适量;香菜,适量;大葱,适量;辣椒油,适量;羊板油,适量","albums":["http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/t/7/6269_379835.jpg"],"steps":["img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_95d65e77b58a1b6b.jpg","step":"1.羊脊骨洗净用刀斩成段。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_a8136c10401a1643.jpg","step":"2.煮锅里倒入清水,放入羊脊骨,羊肉煮开后捞出。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_c7b1c9fc85ddc6de.jpg","step":"3.煮锅里倒入开水,放入羊脊骨生姜块大火煮开后改小火。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_2b284dc30b4f0875.jpg","step":"4.小火煮40分钟,煮至汤色发白。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_c7ade6439eb2db5a.jpg","step":"5.放入羊肉,加入适量的羊板油小火煮30分钟。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_579748e3b0f15963.jpg","step":"6.捞出煮好的羊肉,晾凉后切薄片。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_1550e6f127aa1077.jpg","step":"7.碗里放入葱花,调入精盐。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_a2c965d77b96da70.jpg","step":"8.放入羊肉片,把滚开的羊汤倒入碗里洒上香菜末。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_eea9b807d1dc5995.jpg","step":"9.可以根据喜好调入陈醋放入蒜粒,最后调入辣椒油即可。"]],"totalNum":"9","pn":0,"rn":"1"
* error_code : 0
*/
private String resultcode;
private String reason;
private ResultBean result;
private int error_code;
public String getResultcode()
return resultcode;
public void setResultcode(String resultcode)
this.resultcode = resultcode;
public String getReason()
return reason;
public void setReason(String reason)
this.reason = reason;
public ResultBean getResult()
return result;
public void setResult(ResultBean result)
this.result = result;
public int getError_code()
return error_code;
public void setError_code(int error_code)
this.error_code = error_code;
public static class ResultBean
/**
* data : ["id":"6269","title":"羊肉汤","tags":"增强抵抗力;煮;家常菜;汤;鲁菜","imtro":"邹城人有喝羊汤的习惯,春夏秋冬羊汤馆总断不了食客,春秋天气候干燥要喝,夏天入伏要喝\\u201c伏羊汤\\u201d,阴冷的冬季尤其要喝碗羊汤才够温暖。以至于邀友喝羊汤成为了礼仪;\\u201c二哥,晚上咱们喝羊汤去\\u201d。邹城的羊汤铺遍地开花,以至于单县羊汤、滕州羊汤在邹城都没有了用武之地。我们这里的羊汤做法是最纯的,基本不放煮肉的香料,就用羊骨和羊肉煮成,\\u201c肉嫩汤浓\\u201d是其特色。 煮羊汤要先煮羊骨,把羊骨斩成大段焯水后放一点羊板油用细火煮,煮到汤白味浓时放入羊肉。羊肉煮到用筷子能轻松插穿时就要捞出,久煮的话羊肉过烂就失去了软嫩的口感。 碗里放入葱花或蒜粒,调入精盐,放入切的薄薄的羊肉片。把烧的滚开的羊汤盛到碗里,洒上香菜,再挖上一匙子香辣的用羊油泼成的辣椒油,一个字\\u201c香\\u201d!","ingredients":"山羊肉,500g;羊骨,1000g","burden":"生姜,适量;精盐,适量;香菜,适量;大葱,适量;辣椒油,适量;羊板油,适量","albums":["http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/t/7/6269_379835.jpg"],"steps":["img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_95d65e77b58a1b6b.jpg","step":"1.羊脊骨洗净用刀斩成段。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_a8136c10401a1643.jpg","step":"2.煮锅里倒入清水,放入羊脊骨,羊肉煮开后捞出。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_c7b1c9fc85ddc6de.jpg","step":"3.煮锅里倒入开水,放入羊脊骨生姜块大火煮开后改小火。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_2b284dc30b4f0875.jpg","step":"4.小火煮40分钟,煮至汤色发白。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_c7ade6439eb2db5a.jpg","step":"5.放入羊肉,加入适量的羊板油小火煮30分钟。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_579748e3b0f15963.jpg","step":"6.捞出煮好的羊肉,晾凉后切薄片。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_1550e6f127aa1077.jpg","step":"7.碗里放入葱花,调入精盐。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_a2c965d77b96da70.jpg","step":"8.放入羊肉片,把滚开的羊汤倒入碗里洒上香菜末。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_eea9b807d1dc5995.jpg","step":"9.可以根据喜好调入陈醋放入蒜粒,最后调入辣椒油即可。"]]
* totalNum : 9
* pn : 0
* rn : 1
*/
private String totalNum;
private int pn;
private String rn;
private List<DataBean> data;
public String getTotalNum()
return totalNum;
public void setTotalNum(String totalNum)
this.totalNum = totalNum;
public int getPn()
return pn;
public void setPn(int pn)
this.pn = pn;
public String getRn()
return rn;
public void setRn(String rn)
this.rn = rn;
public List<DataBean> getData()
return data;
public void setData(List<DataBean> data)
this.data = data;
public static class DataBean
/**
* id : 6269
* title : 羊肉汤
* tags : 增强抵抗力;煮;家常菜;汤;鲁菜
* imtro : 邹城人有喝羊汤的习惯,春夏秋冬羊汤馆总断不了食客,春秋天气候干燥要喝,夏天入伏要喝“伏羊汤”,阴冷的冬季尤其要喝碗羊汤才够温暖。以至于邀友喝羊汤成为了礼仪;“二哥,晚上咱们喝羊汤去”。邹城的羊汤铺遍地开花,以至于单县羊汤、滕州羊汤在邹城都没有了用武之地。我们这里的羊汤做法是最纯的,基本不放煮肉的香料,就用羊骨和羊肉煮成,“肉嫩汤浓”是其特色。 煮羊汤要先煮羊骨,把羊骨斩成大段焯水后放一点羊板油用细火煮,煮到汤白味浓时放入羊肉。羊肉煮到用筷子能轻松插穿时就要捞出,久煮的话羊肉过烂就失去了软嫩的口感。 碗里放入葱花或蒜粒,调入精盐,放入切的薄薄的羊肉片。把烧的滚开的羊汤盛到碗里,洒上香菜,再挖上一匙子香辣的用羊油泼成的辣椒油,一个字“香”!
* ingredients : 山羊肉,500g;羊骨,1000g
* burden : 生姜,适量;精盐,适量;香菜,适量;大葱,适量;辣椒油,适量;羊板油,适量
* albums : ["http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/t/7/6269_379835.jpg"]
* steps : ["img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_95d65e77b58a1b6b.jpg","step":"1.羊脊骨洗净用刀斩成段。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_a8136c10401a1643.jpg","step":"2.煮锅里倒入清水,放入羊脊骨,羊肉煮开后捞出。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_c7b1c9fc85ddc6de.jpg","step":"3.煮锅里倒入开水,放入羊脊骨生姜块大火煮开后改小火。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_2b284dc30b4f0875.jpg","step":"4.小火煮40分钟,煮至汤色发白。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_c7ade6439eb2db5a.jpg","step":"5.放入羊肉,加入适量的羊板油小火煮30分钟。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_579748e3b0f15963.jpg","step":"6.捞出煮好的羊肉,晾凉后切薄片。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_1550e6f127aa1077.jpg","step":"7.碗里放入葱花,调入精盐。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_a2c965d77b96da70.jpg","step":"8.放入羊肉片,把滚开的羊汤倒入碗里洒上香菜末。","img":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/63/6269_eea9b807d1dc5995.jpg","step":"9.可以根据喜好调入陈醋放入蒜粒,最后调入辣椒油即可。"]
*/
private String id;
private String title;
private String tags;
private String imtro;
private String ingredients;
private String burden;
private List<String> albums;
private List<StepsBean> steps;
public String getId()
return id;
public void setId(String id)
this.id = id;
public String getTitle(以上是关于fastjson详解的主要内容,如果未能解决你的问题,请参考以下文章