JSON path

阅读: 评论:0

JSON path

JSON path

.md

 

1. 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,  values); }

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

3. 支持语法

JSONPATH描述
$根对象,例如$.name
[num]数组访问,其中num是数字,可以是负数。例如$[0].leader.departments[-1].name
[num0,]数组多个元素访问,其中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.Value(), JSONPath.eval(entity, "$.value")); Assert.ains(entity, "$.value")); Assert.ainsValue(entity, "$.id", 123)); Assert.ainsValue(entity, "$.value", Value())); 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.(0).getName(), (0)); Assert.(1).getName(), (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.(1), (0)); Assert.(2), (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.(0), (0)); Assert.(1), (1)); Assert.(2), (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.(0), (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, Id().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, (0)); assertEquals(1002, (1)); assertEquals(1003, (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"); 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={}", String()); } @Test public void test2() { User user = new User("itguang", "123456", "123@qq"); Entity entity = new Entity(123, user); //判断entity中是否有 data boolean contains = ains(entity, "$.data"); Assert.assertTrue(contains); //判断 entity.data.username 属性值是否为 itguang boolean containsValue = ainsValue(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("通过条件过滤,返回集合的子集={}

本文发布于:2024-01-28 05:43:22,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063918065209.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:JSON   path
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23