今天学习jsonpath解析的时候,才发现jsonpath功能很强大,比xpath还强大
官网wiki
https://github.com/alibaba/fastjson/wiki/JSONPath
支持语法
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 rlike '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和数组 |
keySet() | 获取Map的keySet或者对象的非空属性名称。例如$.val.keySet() 支持类型:Map和普通对象 不支持:Collection和数组(返回null) |
. | 属性访问,例如$.name |
.. | deepScan属性访问,例如$..name |
* | 对象的所有属性,例如$.leader.* |
['key'] | 属性访问。例如$['name'] |
['key0','key1'] | 多个属性访问。例如$['id','name'] |
取相对路径 $..name 类似xpath的 //name
直接光秃秃的数组根目录
$[0:2][age='35']
解析java对象 以后可能会有用
@Test
public void testassert(){
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String,String> map = new HashMap<String,String>();
map.put("name", "zhouyong");
map.put("age", "35");
map.put("tel", "15380222222");
Map<String,String> map2 = new HashMap<String,String>();
map2.put("name", "zhouxingchi");
map2.put("age", "60");
map2.put("tel", "05755222222");
list.add(map);
list.add(map2);
List<String> name = (List<String>) JSONPath.eval(list, "$.name[0:2]");
System.out.println(name);
Object obj = JSONPath.eval(list, "$[0:2][age='35']");
System.out.println(obj);
}
输出
[zhouyong, zhouxingchi]
[{"name":"zhouyong","tel":"15380222222","age":"35"}]
这段在网上抄的 测试了下 可以的
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"isbn": "0-553-21311-3"
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
@Test
public void testjosnpath() {
String jsonStr = "{ \"store\": {\"book\": [{ \"category\": \"reference\"," +
"\"author\": \"Nigel Rees\",\"title\": \"Sayings of the Century\"," +
"\"price\": 8.95},{ \"category\": \"fiction\",\"author\": \"Evelyn Waugh\"," +
"\"title\": \"Sword of Honour\",\"price\": 12.99,\"isbn\": \"0-553-21311-3\"" +
"}],\"bicycle\": {\"color\": \"red\",\"price\": 19.95}}}";
System.out.println(jsonStr);
JSONObject jsonObject = JSON.parseObject(jsonStr);
List<String> string = (List<String>)JSONPath.eval(jsonObject, "$.store.book[category in ('reference')].title");
System.out.println("========obj====="+string.get(0));
Object obj = JSONPath.eval(jsonObject, "$.store.book.size()");
System.out.println("testtestestse===="+obj);
System.out.println("book数目:"+ JSONPath.eval(jsonObject, "$.store.book.size()") );
System.out.println("第一本书的title:"+JSONPath.eval(jsonObject,"$.store.book[0].title"));
System.out.println("第一本书的category和author:"+JSONPath.eval(jsonObject,"$.store.book[0]['category','author']"));
System.out.println("price>10的书:"+JSONPath.eval(jsonObject,"$.store.book[price>10]"));
System.out.println("price>8的书的标题:"+JSONPath.eval(jsonObject,"$.store.book[price>8]"));
System.out.println("price>7的书:"+JSONPath.eval(jsonObject,"$.store.book[price>7]"));
System.out.println("price>7的书的标题:"+JSONPath.eval(jsonObject,"$.store.book[price>7].title"));
//不带单引号会出现Exception in thread "main" java.lang.UnsupportedOperationException 异常
System.out.println("书的标题为Sayings of the Century:"+JSONPath.eval(jsonObject,"$.store.book[title='Sayings of the Century']"));
System.out.println("bicycle的所有属性:"+JSONPath.eval(jsonObject,"$.store.bicycle.*"));
System.out.println("bicycle:"+JSONPath.eval(jsonObject,"$.store.bicycle"));
}
输出
========obj=====Sayings of the Century
testtestestse====2
book数目:2
第一本书的title:Sayings of the Century
第一本书的category和author:[reference, Nigel Rees]
price>10的书:[{"author":"Evelyn Waugh","price":12.99,"isbn":"0-553-21311-3","category":"fiction","title":"Sword of Honour"}]
price>8的书的标题:[{"author":"Evelyn Waugh","price":12.99,"isbn":"0-553-21311-3","category":"fiction","title":"Sword of Honour"}]
price>7的书:[{"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"}]
price>7的书的标题:["Sayings of the Century","Sword of Honour"]
书的标题为Sayings of the Century:[{"author":"Nigel Rees","price":8.95,"category":"reference","title":"Sayings of the Century"}]
bicycle的所有属性:[red, 19.95]
bicycle:{"color":"red","price":19.95}
还有更多神奇的写法,学习中
下面这种估计要只能用绝对路径的方式写 相当于and
$.ordOfferProdInstRels[roleId=329][statusCd!='1300'].roleId[0]
$.resultObject[createOrgId in (251004534,251004535,251004536,11238203)].createOrgId