https://github.com/itguang/gitbook-smile/blob/master/springboot-fastjson/fastjson%E4%B9%8BJSONPath%E4%BD%BF%E7%94%A8.md

1. JSONPath介绍

官网地址: https://github.com/alibaba/fastjson/wiki/JSONPath

fastjson 1.2.0之后的版本支持JSONPath。这是一个很强大的功能,可以在java框架中当作对象查询语言(OQL)来使用。

2. API

package com.alibaba.fastjson;

public class JSONPath {
// 求值,静态方法
public static Object eval(Object rootObject, String path); // 计算Size,Map非空元素个数,对象非空元素个数,Collection的Size,数组的长度。其他无法求值返回-1
public static int size(Object rootObject, String path); // 是否包含,path中是否存在对象
public static boolean contains(Object rootObject, String path) { } // 是否包含,path中是否存在指定值,如果是集合或者数组,在集合中查找value是否存在
public static boolean containsValue(Object rootObject, String path, Object value) { } // 修改制定路径的值,如果修改成功,返回true,否则返回false
public static boolean set(Object rootObject, String path, Object value) {} // 在数组或者集合中添加元素
public static boolean array_add(Object rootObject, String path, Object... values);
}

建议缓存JSONPath对象,这样能够提高求值的性能。

3. 支持语法

JSONPATH 描述
$ 根对象,例如$.name
[num] 数组访问,其中num是数字,可以是负数。例如$[0].leader.departments[-1].name
[num0,num1,num2...] 数组多个元素访问,其中num是数字,可以是负数,返回数组中的多个元素。例如$[0,3,-2,5]
[start:end] 数组范围访问,其中start和end是开始小表和结束下标,可以是负数,返回数组中的多个元素。例如$[0:5]
[start:end :step] 数组范围访问,其中start和end是开始小表和结束下标,可以是负数;step是步长,返回数组中的多个元素。例如$[0:5:2]
[?(key)] 对象属性非空过滤,例如$.departs[?(name)]
[key > 123] 数值类型对象属性比较过滤,例如$.departs[id >= 123],比较操作符支持=,!=,>,>=,<,<=
[key = '123'] 字符串类型对象属性比较过滤,例如$.departs[name = '123'],比较操作符支持=,!=,>,>=,<,<=
[key like 'aa%'] 字符串类型like过滤,
例如$.departs[name like 'sz*'],通配符只支持% 
支持not like
[key rlike 'regexpr'] 字符串类型正则匹配过滤,
例如departs[name like 'aa(.)*'],
正则语法为jdk的正则语法,支持not rlike
[key in ('v0', 'v1')] IN过滤, 支持字符串和数值类型 
例如: 
$.departs[name in ('wenshao','Yako')] 
$.departs[id not in (101,102)]
[key between 234 and 456] BETWEEN过滤, 支持数值类型,支持not between 
例如: 
$.departs[id between 101 and 201]
$.departs[id not between 101 and 201]
length() 或者 size() 数组长度。例如$.values.size() 
支持类型java.util.Map和java.util.Collection和数组
. 属性访问,例如$.name
.. deepScan属性访问,例如$..name
* 对象的所有属性,例如$.leader.*
['key'] 属性访问。例如$['name']
['key0','key1'] 多个属性访问。例如$['id','name']

以下两种写法的语义是相同的:

$.store.book[0].title

$['store']['book'][0]['title']

4. 语法示例

JSONPath 语义
$ 根对象
$[-1] 最后元素
$[:-2] 第1个至倒数第2个
$[1:] 第2个之后所有元素
$[1,2,3] 集合中1,2,3个元素

5. API 示例

5.1 例1

public void test_entity() throws Exception {
Entity entity = new Entity(123, new Object()); Assert.assertSame(entity.getValue(), JSONPath.eval(entity, "$.value"));
Assert.assertTrue(JSONPath.contains(entity, "$.value"));
Assert.assertTrue(JSONPath.containsValue(entity, "$.id", 123));
Assert.assertTrue(JSONPath.containsValue(entity, "$.value", entity.getValue()));
Assert.assertEquals(2, JSONPath.size(entity, "$"));
Assert.assertEquals(0, JSONPath.size(new Object[], "$"));
} public static class Entity {
private Integer id;
private String name;
private Object value; public Entity() {}
public Entity(Integer id, Object value) { this.id = id; this.value = value; }
public Entity(Integer id, String name) { this.id = id; this.name = name; }
public Entity(String name) { this.name = name; } public Integer getId() { return id; }
public Object getValue() { return value; }
public String getName() { return name; } public void setId(Integer id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setValue(Object value) { this.value = value; }
}

5.2 例2

读取集合多个元素的某个属性

List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083")); List<String> names = (List<String>)JSONPath.eval(entities, "$.name"); // 返回enties的所有名称
Assert.assertSame(entities.get(0).getName(), names.get(0));
Assert.assertSame(entities.get(1).getName(), names.get(1));

5.3 例3

返回集合中多个元素

List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083"));
entities.add(new Entity("Yako")); List<Entity> result = (List<Entity>)JSONPath.eval(entities, "[1,2]"); // 返回下标为1和2的元素
Assert.assertEquals(2, result.size());
Assert.assertSame(entities.get(1), result.get(0));
Assert.assertSame(entities.get(2), result.get(1));

5.4 例4

按范围返回集合的子集

List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083"));
entities.add(new Entity("Yako")); List<Entity> result = (List<Entity>)JSONPath.eval(entities, "[0:2]"); // 返回下标从0到2的元素
Assert.assertEquals(3, result.size());
Assert.assertSame(entities.get(0), result.get(0));
Assert.assertSame(entities.get(1), result.get(1));
Assert.assertSame(entities.get(2), result.get(1));

5.5 例5

通过条件过滤,返回集合的子集

List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity(1001, "ljw2083"));
entities.add(new Entity(1002, "wenshao"));
entities.add(new Entity(1003, "yakolee"));
entities.add(new Entity(1004, null)); List<Object> result = (List<Object>) JSONPath.eval(entities, "[id in (1001)]");
Assert.assertEquals(1, result.size());
Assert.assertSame(entities.get(0), result.get(0));

5.6 例6

根据属性值过滤条件判断是否返回对象,修改对象,数组属性添加元素

Entity entity = new Entity(1001, "ljw2083");
Assert.assertSame(entity , JSONPath.eval(entity, "[id = 1001]"));
Assert.assertNull(JSONPath.eval(entity, "[id = 1002]")); JSONPath.set(entity, "id", 123456); //将id字段修改为123456
Assert.assertEquals(123456, entity.getId().intValue()); JSONPath.set(entity, "value", new int[0]); //将value字段赋值为长度为0的数组
JSONPath.arrayAdd(entity, "value", 1, 2, 3); //将value字段的数组添加元素1,2,3

5.7 例7

Map root = Collections.singletonMap("company", //
Collections.singletonMap("departs", //
Arrays.asList( //
Collections.singletonMap("id",
1001), //
Collections.singletonMap("id",
1002), //
Collections.singletonMap("id", 1003) //
) //
)); List<Object> ids = (List<Object>) JSONPath.eval(root, "$..id");
assertEquals(3, ids.size());
assertEquals(1001, ids.get(0));
assertEquals(1002, ids.get(1));
assertEquals(1003, ids.get(2));

具体用例测试请看下面:

/**
* @author itguang
* @create 2017-12-10 10:03
**/ @RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class JSONpathControllerTest { @Test
public void test() { User user = new User("itguang", "123456", "123@qq.com");
String username = (String) JSONPath.eval(user, "$.username"); log.info("$.username = {}", username); Entity entity = new Entity(123, user);
User user1 = (User) JSONPath.eval(entity, "$.value");
log.info("user={}", user1.toString()); } @Test
public void test2() { User user = new User("itguang", "123456", "123@qq.com");
Entity entity = new Entity(123, user); //判断entity中是否有 data
boolean contains = JSONPath.contains(entity, "$.data");
Assert.assertTrue(contains); //判断 entity.data.username 属性值是否为 itguang
boolean containsValue = JSONPath.containsValue(entity, "$.data.username", "itguang");
Assert.assertTrue(containsValue); Assert.assertEquals(2, JSONPath.size(entity, "$")); } @Test
public void test3() { List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("逻辑"));
entities.add(new Entity("叶文杰"));
entities.add(new Entity("程心")); //返回集合中多个元素
List<String> names = (List<String>) JSONPath.eval(entities, "$.name");
log.info("返回集合中多个元素names={}", names); //返回下标 0 和 2 的元素
List<Entity> result = (List<Entity>) JSONPath.eval(entities, "[0,2]");
log.info("返回下标 0 和 2 的元素={}", result); // 返回下标从0到2的元素
List<Entity> result2 = (List<Entity>) JSONPath.eval(entities, "[0:2]"); log.info("返回下标从0到2的元素={}", result2); } @Test
public void test4() { List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity(1001, "逻辑"));
entities.add(new Entity(1002, "程心"));
entities.add(new Entity(1003, "叶文杰"));
entities.add(new Entity(1004, null)); //通过条件过滤,返回集合的子集 List<Entity> result = (List<Entity>) JSONPath.eval(entities, "[id in (1001)]");
log.info("通过条件过滤,返回集合的子集={}", result); } /**
* 使用JSONPrase 解析JSON字符串或者Object对象
* <p>
* read(String json, String path)//直接使用json字符串匹配
* <p>
* eval(Object rootObject, String path) //直接使用 对象匹配
* <p>
* <p>
* {"store":{"bicycle":{"color":"red","price":19.95},"book":[{"author":"Nigel Rees","price":8.95,"category":"reference","title":"Sayings of the Century"},{"author":"Evelyn Waugh","price":12.99,"isbn":"0-553-21311-3","category":"fiction","title":"Sword of Honour"}]}}
*/
@Test
public void test5() { String jsonStr = "{\n" +
" \"store\": {\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" },\n" +
" \"book\": [\n" +
" {\n" +
" \"author\": \"刘慈欣\",\n" +
" \"price\": 8.95,\n" +
" \"category\": \"科幻\",\n" +
" \"title\": \"三体\"\n" +
" },\n" +
" {\n" +
" \"author\": \"itguang\",\n" +
" \"price\": 12.99,\n" +
" \"category\": \"编程语言\",\n" +
" \"title\": \"go语言实战\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}"; JSONObject jsonObject = JSON.parseObject(jsonStr); log.info(jsonObject.toString()); //得到所有的书
List<Book> books = (List<Book>) JSONPath.eval(jsonObject, "$.store.book");
log.info("books={}", books); //得到所有的书名
List<String> titles = (List<String>) JSONPath.eval(jsonObject, "$.store.book.title");
log.info("titles={}", titles); //第一本书title
String title = (String) JSONPath.read(jsonStr, "$.store.book[0].title");
log.info("title={}", title); //price大于10元的book
List<Book> list = (List<Book>) JSONPath.read(jsonStr, "$.store.book[price > 10]");
log.info("price大于10元的book={}",list); //price大于10元的title
List<String> list2 =(List<String>) JSONPath.read(jsonStr, "$.store.book[price > 10].title");
log.info("price大于10元的title={}",list2); //category(类别)为科幻的book
List<Book> list3 = (List<Book>) JSONPath.read(jsonStr,"$.store.book[category = '科幻']");
log.info("category(类别)为科幻的book={}",list3); //bicycle的所有属性值 Collection<String> values = (Collection<String>) JSONPath.eval(jsonObject, "$.store.bicycle.*"); log.info("bicycle的所有属性值={}",values); //bicycle的color和price属性值
List<String> read =(List<String>) JSONPath.read(jsonStr, "$.store.bicycle['color','price']"); log.info("bicycle的color和price属性值={}",read); } }

JSON path的更多相关文章

  1. 继续说一下openjson 以及 json path 的使用 (2)

    在openjson 里面,其实是可以把数据类型array里面的值遍历出来的,举个栗子 ) = N' {"name":"test", "obj" ...

  2. 【JEMTER】后置处理器JSON Path Extractor获取server端返回的json中某项值

    需求1:点击所有报表模板时,server端返回所有报表模板的ID(templateId),测试时需要下载某个模板生成的报表 需求2:点击单个报表模板时,server端返回这个报表模板下的所有报表ID( ...

  3. jmeter里json path postprocessor的用法

    后置处理器添加 json path postprocessor. 用处: 当前接口响应返回的json中提取内容,作为变量可以在不同的请求中传递. 如下,从登陆接口返回的json中提取user id,变 ...

  4. JMeter 插件 Json Path 解析HTTP响应JSON数据

    一.基本简介 JMeter 是一个不错的负载和性能测试工具,我们也用来做 HTTP API 接口测试.我们的 API 返回结果为JSON数据格式.JSON 简介,JSON 教程. JSON 已经成为数 ...

  5. SQL FOR JSON PATH 返回 json

    --直接返回 age FOR JSON PATH --返回值 [{"name":"张学友","age":60}] select c1, c2 ...

  6. Jmeter 中JSON Path Extractor高级用法

    好久没玩jemter了,由于项目原因又重新拾起.在使用JSON Path Extractor(jmeter 4.0已经默认支持了,4.0以下要自行安装插件)时,可以进行条件过滤,不用再自行写shell ...

  7. JMeter 插件 Json Path 解析 HTTP 响应 JSON 数据(转)

    JMeter 是一个不错的负载和性能测试工具,我们也用来做 HTTP API 接口测试.我们的 API 返回结果为 JSON 数据格式.JSON 简介,JSON 教程. JSON 已经成为数据交换格式 ...

  8. JMeter----正则表达式&JSON Path Extractor

    最近在用JMerter给公司一个项目做性能测试,期间遇到要提取上一个接口返回的数据作为下个接口的请求.这里做下记录 如图所示,需要将“扫描二维码”接口请求的返回值中的data部分,作为“处理提交码值” ...

  9. 使用json path设置关联

    与正则表达式相比,这种方法会更简单一些: json path是在返回的是K-V的格式中根据key进行的关联,如果压的接口返回的是json的话,使用json path比较方便,如果不是json的话,就使 ...

  10. JSON Path表达式

      JSON Path 描述 $ 表示根元素 @ 表示当前节点 .  表示子节点 .. 选择所有符合条件的节点 * 所有节点 [] 迭代器标识,如数组下标 [,] 支持迭代器中多选 [start:en ...

随机推荐

  1. C#编程(二)

    C#中的变量 例如:int i;//声明一个int类型的变量,变量名是 i;在未为该变量进行赋值操作前,禁止使用该变量.使用(=)给变量赋值,在声明之后可以 i=10来赋值.也可以在声明一个变量的同时 ...

  2. Android实例剖析笔记(三)

    摘要:点介绍Activity的生命周期,通过一个简单的实验来摸索状态转换的机制 Activity的生命周期 Activity类中有许多onXXX形式的函数可以重载,比如onCreate,onStart ...

  3. 无损转换Image为Icon z

    如题,市面上常见的方法是: var handle = bmp.GetHicon(); //得到图标句柄 return Icon.FromHandle(handle); //通过句柄得到图标 此法的问题 ...

  4. 算法:希尔排序(Shell Sort)

    背景 在三种简单的排序算法中(冒泡.选择和插入)插入排序的算法最好,不过插入过程可能需要进行大量的移动,如何尽可能少的移动元素呢?希尔排序正是基于对这个问题的思考而想出来的,考虑到希尔排序对已排序数组 ...

  5. python笔记25-mock-server之moco

    前言 mock除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的时候,需要服务端提供API接口 此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发会自己mock一个api服务端,自 ...

  6. python接口自动化5-Json数据处理

    前言 有些post的请求参数是json格式的,这个前面第二篇post请求里面提到过,需要导入json模块处理. 一般常见的接口返回数据也是json格式的,我们在做判断时候,往往只需要提取其中几个关键的 ...

  7. putty如何退出全屏模式

    putty有全屏模式,有单击标题栏,在弹出餐单中有fullscreen选项. 全屏模式看起来挺不错. 退出全屏模式的方式有点特别.Esc不起作用.Alt+Enter也不起作用. 要退出的时候需要把鼠标 ...

  8. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  9. Python kmean

    # -*- coding: utf-8 -*-from sklearn.cluster import KMeansfrom sklearn.externals import joblibimport ...

  10. HDU1561:The more, The Better(树形DP+01背包)

    Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有 ...