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']
posted @ 2024-01-10 15:42  月色深潭  阅读(45)  评论(0编辑  收藏  举报