JSON中fastjson、jackson、gson如何选择
作者:云深i不知处
JSON具有表达简洁、层级清晰的特点,目前广泛应用在数据的通信传输中,尤其前后端的交互,几乎都是使用JSON实现的。例如下面的数据:
{
"code" : 0,
"kind" : "Electronics",
"list" : [{
"name" : "computer",
"price" : 4500,
"size" : 60
}, {
"name" : "iphone",
"price" : 6000,
"size" : 55
}, {
"name" : "watch",
"price" : 500,
"size" : 35
}
]
}
在Java中,JSON的解析方式很多,例如fastjson(阿里)、Gson(谷歌)、jackjson等。
1 fastjson
阿里的fastjson目前是应用最广泛的,在maven项目中的pom依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
直接贴上一些常用的JSON解析方法:
public class Mytest {
public static void main(String[] args) {
String goodsData = "{\"code\":0,\"kind\":\"Electronics\",\"list\":[{\"name\":\"computer\",\"price\":4500,\"size\":60},{\"name\":\"iphone\",\"price\":6000,\"size\":55},{\"name\":\"watch\",\"price\":500,\"size\":35}]}";
String goodsListData = "[{\"name\":\"computer\",\"price\":4500,\"size\":60},{\"name\":\"iphone\",\"price\":6000,\"size\":55}]";
// 将符合格式的字符串解析成JSONObject
JSONObject goodsObject = JSON.parseObject(goodsData);
// 将符合格式的字符串解析成JSONArray
JSONArray goodsArray = JSON.parseArray(goodsListData);
// 在JSONObject中获取JSONArray
JSONArray goodsList = goodsObject.getJSONArray("list");
// 在JSONArray中根据索引获取JSONObject
JSONObject goods = goodsList.getJSONObject(0);
// 在JSONObject中获取String
String goodsName = goods.getString("name");
// 在JSONObject中获取Integer
Integer goodsPrice = goods.getInteger("price");
System.out.println("goodsArray:" + goodsArray);
System.out.println("goods:" + goods);
System.out.println("goodsName:" + goodsName);
System.out.println("goodsPrice:" + goodsPrice);
}
}
输出结果:
goodsArray:[{"name":"computer","price":4500,"size":60},{"name":"iphone","price":6000,"size":55}]
goods:{"name":"computer","price":4500,"size":60}
goodsName:computer
goodsPrice:4500
fastjson目前已支持jsonpath解析:JSONPath
2 jsoncode
jsoncode的maven地址如下:
<dependency>
<groupId>cn.miludeer</groupId>
<artifactId>jsoncode</artifactId>
<version>1.2.4</version>
</dependency>
jsoncode对于json的解析比起fastjson来,更加直接简洁,代码如下:
import cn.miludeer.jsoncode.JsonCode;
public class Test {
public static void main(String[] args) {
String string = "{\"code\" : 0,\"data\" : {\"kind\" : \"Electronics\",\"list\" : [{\"name\" : \"computer\",\"price\" : 4500,\"size\" : 55}, {\"name\" : \"iphone\",\"price\" : 6000,\"size\" : 60}]}}";
String[] list = JsonCode.getValueList(string, "$.data.list");
String kind = JsonCode.getValue(string, "$.data.kind");
System.out.println("list:" + list[1]);
System.out.println("kind:" + kind);
}
}
输出结果:
list:{"name" : "iphone","price" : 6000,"size" : 60}
kind:Electronics
jsoncode对比fastjson,在便利性上有了很大提升,但仍有局限性,只能单独处理JSONArray,无法处理JSONObject和JSONArray混合的场景。
3 jsonpath
前面两种json解析都有一定的不足之处,幸好,还有jsonpath这一款神器。首先,它的maven地址是:
<!-- https://mvnrepository.com/artifact/io.gatling/jsonpath -->
<dependency>
<groupId>io.gatling</groupId>
<artifactId>jsonpath_2.11</artifactId>
<version>0.6.4</version>
</dependency>
准备如下的JSON测试数据:
{
"code" : 0,
"data" : {
"kind" : "Electronics",
"list" : [{
"name" : "computer",
"price" : 4500,
"size" : 55
}, {
"name" : "iphone",
"price" : 6000,
"size" : 60
}, {
"name" : "watch",
"price" : 8000,
"size" : 30
}
]
}
}
jsonpath提供了非常丰富便捷的解析表达式,以上面的json串为例,演示几个示例:
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import java.util.List;
/**
* @author guozhengMu
* @version 1.0
* @date 2019/3/26 18:38
* @description
* @modify
*/
public class Test {
public static void main(String[] args) {
String string = "{\"code\" : 0,\"data\" : {\"kind\" : \"Electronics\",\"list\" : [{\"name\" : \"computer\",\"price\" : 4500,\"size\" : 55}, {\"name\" : \"iphone\",\"price\" : 6000,\"size\" : 60},{\"name\" : \"watch\",\"price\" : 8000,\"size\" : 30}]}}";
ReadContext context = JsonPath.parse(string);
// 获取单个值
String name = context.read("$.data.list[0].name");
// 获取JSONArray所有name
List<String> names = context.read("$.data.list[*].name");
// 获取0、2
List<String> names2 = context.read("$.data.list[0,2].name");
// 获取0-2(不含2)
List<String> names3 = context.read("$.data.list[0:2].name");
System.out.println("name:" + name);
System.out.println("names:" + names);
System.out.println("names2:" + names2);
System.out.println("names3:" + names3);
}
}
输出结果:
name:computer
names:["computer","iphone","watch"]
names2:["computer","watch"]
names3:["computer","iphone"]
表达式汇总:
| 序号 | 表达式 | 输出结果 | 作用 |
|---|---|---|---|
| 1 | $.data.list[0].name | String:computer | 获取单个value |
| 2 | $.data.list[*].name | List:[“computer”,“iphone”,“watch”] | 获取全部value |
| 3 | $.data.list[0,2].name | List:[“computer”,“watch”] | 获取特定索引的value |
| 4 | $.data.list[1:].name | List:[“iphone”,“watch”] | 获取索引之后的所有value(含该索引) |
| 5 | $.data.list[:2].name | List:[“computer”,“iphone”] | 获取索引之前的所有value(不含该索引) |
| 6 | $.data.list[?(@.price>6500)] | List:[{“name”:“iphone”,“price”:6000,“size”:60},{“name”:“watch”,“price”:8000,“size”:30}] | 根据条件筛选 |
| 7 | $.data.list[?(@.name == ‘computer')] | [{“name”:“computer”,“price”:4500,“size”:55}] | 根据条件筛选 |
| 8 | $.data.list[?(@.name)] | List:[{“name”:“computer”,“price”:4500,“size”:55},{“name”:“iphone”,“price”:6000,“size”:60},{“name”:“watch”,“price”:8000,“size”:30}] | 获取含有name属性的元素 |
| 9 | $.data.list[?(@.price > 5000 && @.size > 30)] | List:[{“name”:“iphone”,“price”:6000,“size”:60}] | 多条件查询(且) |
| 10 | $.data.list[?(@.price < 7000 || @.size <= 30)] | List:[{“name”:“iphone”,“price”:6000,“size”:60},{“name”:“watch”,“price”:8000,“size”:30}] | 多条件查询(或) |
| 11 | $.data.list.length() | Integer:3 | 查询JSONArray长度 |
| 12 | $.max($.data.list[0].price,$.data.list[1].price) | Object:6000.0 | 获取最大值,最小值:min,平均值:avg,标准差:stddev |
| 13 | $.data.list[?(@.price > 5000 && @.size > 30)] | List:[{“name”:“iphone”,“price”:6000,“size”:60}] | 多条件查询(且) |
到此这篇关于JSON中fastjson、jackson、gson如何选择的文章就介绍到这了,更多相关fastjson jackson gson内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- Jackson使用示例-Bean、XML、Json之间相互转换
- 一篇文章了解Jackson注解@JsonFormat及失效解决办法
- Java中对象 和 json 互转四种方式 json-lib、Gson、FastJson、Jackson
- 利用Jackson解决Json序列化和反序列化问题
- Java利用Jackson轻松处理JSON序列化与反序列化
- Jackson中json格式的字符串与对象的互相转换方式
- 如何自定义Jackson序列化 @JsonSerialize
- jackson 如何将实体转json json字符串转实体
- 使用jackson实现对象json之间的相互转换(spring boot)
- 使用Jackson-json解析一个嵌套的json字符串
- Jackson库进行JSON 序列化时遇到了无限递归(Infinite Recursion)的问题及解决方案
