JSON:fastjson、jackson、gson的选择之惑

数据结构之JSON浅析


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].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}]多条件查询(且)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云深i不知处

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值