阅读 202

JSON中fastjson、jackson、gson如何选择

在Java中,JSON的解析方式很多,例如fastjson(阿里)、Gson(谷歌)、jackjson等,本文主要介绍了JSON中fastjson、jackson、gson如何选择,具有一定的参考价值,感兴趣的可以了解一下

目录
  • 1 fastjson

  • 2 jsoncode

  • 3 jsonpath

JSON具有表达简洁、层级清晰的特点,目前广泛应用在数据的通信传输中,尤其前后端的交互,几乎都是使用JSON实现的。例如下面的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
 "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依赖:

1
2
3
4
5
<dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>fastjson</artifactId>
 <version>1.2.47</version>
</dependency>

直接贴上一些常用的JSON解析方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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地址如下:

1
2
3
4
5
<dependency>
       <groupId>cn.miludeer</groupId>
       <artifactId>jsoncode</artifactId>
       <version>1.2.4</version>
</dependency>

jsoncode对于json的解析比起fastjson来,更加直接简洁,代码如下:

1
2
3
4
5
6
7
8
9
10
11
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地址是:

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/io.gatling/jsonpath -->
<dependency>
    <groupId>io.gatling</groupId>
    <artifactId>jsonpath_2.11</artifactId>
    <version>0.6.4</version>
</dependency>

准备如下的JSON测试数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
 "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串为例,演示几个示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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].nameString:computer获取单个value
2$.data.list[*].nameList:[“computer”,“iphone”,“watch”]获取全部value
3$.data.list[0,2].nameList:[“computer”,“watch”]获取特定索引的value
4$.data.list[1:].nameList:[“iphone”,“watch”]获取索引之后的所有value(含该索引)
5$.data.list[:2].nameList:[“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如何选择的文章就介绍到这了

原文链接:https://blog.csdn.net/mu_wind/article/details/93725063


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐