JOSN字符串字段遍历(json-path)
官网
https://github.com/json-path/JsonPath
依赖
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.5.0</version>
</dependency>
json字符串样例
{
"execId": "123456",
"params": [{
"databaseName": [{
"tableName1": [{
"id": 123,
"name": "aaa"
}, {
"id": 456,
"name": "bbb"
}]
}, {
"tableName2": [{
"id": 123,
"name": "nnn"
}]
}]
}, {
"databaseName2": [{
"tableName1": [{
"id": 123,
"name": "aaa"
}, {
"id": 456,
"name": "bbb"
}]
}]
}]
}
遍历所有字段
String jsonString = "{\"execId\":\"123456\",\"params\":[{\"databaseName\":[{\"tableName1\":[{\"id\":123,\"name\":\"aaa\"},{\"id\":456,\"name\":\"bbb\"}]},{\"tableName2\":[{\"id\":123,\"name\":\"nnn\"}]}]},{\"databaseName2\":[{\"tableName1\":[{\"id\":123,\"name\":\"aaa\"},{\"id\":456,\"name\":\"bbb\"}]}]}]}";
Configuration configuration = Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> paths = JsonPath.using(configuration).parse(jsonString).read("$..*");
for (String path : paths) {
System.out.println(path);
}
结果
$['execId']
$['params']
$['params'][0]
$['params'][1]
$['params'][0]['databaseName']
$['params'][0]['databaseName'][0]
$['params'][0]['databaseName'][1]
$['params'][0]['databaseName'][0]['tableName1']
$['params'][0]['databaseName'][0]['tableName1'][0]
$['params'][0]['databaseName'][0]['tableName1'][1]
$['params'][0]['databaseName'][0]['tableName1'][0]['id']
$['params'][0]['databaseName'][0]['tableName1'][0]['name']
$['params'][0]['databaseName'][0]['tableName1'][1]['id']
$['params'][0]['databaseName'][0]['tableName1'][1]['name']
$['params'][0]['databaseName'][1]['tableName2']
$['params'][0]['databaseName'][1]['tableName2'][0]
$['params'][0]['databaseName'][1]['tableName2'][0]['id']
$['params'][0]['databaseName'][1]['tableName2'][0]['name']
$['params'][1]['databaseName2']
$['params'][1]['databaseName2'][0]
$['params'][1]['databaseName2'][0]['tableName1']
$['params'][1]['databaseName2'][0]['tableName1'][0]
$['params'][1]['databaseName2'][0]['tableName1'][1]
$['params'][1]['databaseName2'][0]['tableName1'][0]['id']
$['params'][1]['databaseName2'][0]['tableName1'][0]['name']
$['params'][1]['databaseName2'][0]['tableName1'][1]['id']
$['params'][1]['databaseName2'][0]['tableName1'][1]['name']
有子节点的路径
String jsonString = "{\"execId\":\"123456\",\"params\":[{\"databaseName\":[{\"tableName1\":[{\"id\":123,\"name\":\"aaa\"},{\"id\":456,\"name\":\"bbb\"}]},{\"tableName2\":[{\"id\":123,\"name\":\"nnn\"}]}]},{\"databaseName2\":[{\"tableName1\":[{\"id\":123,\"name\":\"aaa\"},{\"id\":456,\"name\":\"bbb\"}]}]}]}";
Configuration configuration = Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> paths_ex = JsonPath.using(configuration).parse(jsonString).read("$..*[?(@.length != 0)]");
for (String path : paths_ex) {
System.out.println(path);
}
结果:
$['params'][0]
$['params'][1]
$['params'][0]
$['params'][1]
$['params'][0]['databaseName'][0]
$['params'][0]['databaseName'][1]
$['params'][0]['databaseName'][0]
$['params'][0]['databaseName'][1]
$['params'][0]['databaseName'][0]['tableName1'][0]
$['params'][0]['databaseName'][0]['tableName1'][1]
$['params'][0]['databaseName'][0]['tableName1'][0]
$['params'][0]['databaseName'][0]['tableName1'][1]
$['params'][0]['databaseName'][1]['tableName2'][0]
$['params'][0]['databaseName'][1]['tableName2'][0]
$['params'][1]['databaseName2'][0]
$['params'][1]['databaseName2'][0]
$['params'][1]['databaseName2'][0]['tableName1'][0]
$['params'][1]['databaseName2'][0]['tableName1'][1]
$['params'][1]['databaseName2'][0]['tableName1'][0]
$['params'][1]['databaseName2'][0]['tableName1'][1]
自定义匹配路径
String jsonString = "{\"execId\":\"123456\",\"params\":[{\"databaseName\":[{\"tableName1\":[{\"id\":123,\"name\":\"aaa\"},{\"id\":456,\"name\":\"bbb\"}]},{\"tableName2\":[{\"id\":123,\"name\":\"nnn\"}]}]},{\"databaseName2\":[{\"tableName1\":[{\"id\":123,\"name\":\"aaa\"},{\"id\":456,\"name\":\"bbb\"}]}]}]}";
Configuration configuration = Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> paths_s = JsonPath.using(configuration).parse(jsonString).read("$.params[*].databaseName2[*].tableName1[*].id");
System.out.println("================");
for (String path : paths_s) {
System.out.println(path);
}
结果:
$['params'][1]['databaseName2'][0]['tableName1'][0]['id']
$['params'][1]['databaseName2'][0]['tableName1'][1]['id']
本文来自博客园,作者:月色深潭,交流群:733423266,转载请注明原文链接:https://www.cnblogs.com/moonpool/p/17956549