json Java JsonPath获取基本类型属性的所有路径
我想从属性类型为基元(而不是对象或数组)的Json文件中查找所有Json路径
考虑<强> jSONSROCT<<强>:
{
"header": {
"version": 2,
"original": "ori",
"parent": "par",
"eventId": 11,
"correlation": "uuid",
"timestamp": "03.04.2020",
"local": true,
"location": {
"facility": {
"id": 3,
"type": "en"
}
}
},
"body": {
"field": 3
}
}
我使用以下代码:
Configuration configuration = Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> paths = JsonPath.using(configuration).parse(jsonString).read("$..*");
实际结果:模式“$…*”返回json中存在的所有路径:
- $['header']
- $['body']
- $['header']['version']
- $['header']['original']
- $['header']['parent']
- $['header']['eventId']
- $['header']['correlation']
- $['header']['timestamp']
- $['header']['local']
- $['header']['location']
- $['header']['location']['facility']
- $['header']['location']['facility']['id']
- $['header']['location']['facility']['type']
- $['body']['field']
预期结果:我只需要得到以下结果:
- $['header']['version']
- $['header']['original']
- $['header']['parent']
- $['header']['eventId']
- $['header']['correlation']
- $['header']['timestamp']
- $['header']['local']
- $['header']['location']['facility']['id']
- $['header']['location']['facility']['type']
- $['body']['field']
过滤器应该是通用的,这样它就可以解析作为输入的任何json格式
# 1 楼答案
您可以使用下面的JSONPath,它将给出包含对象或数组的路径,然后您可以将其从整个JSONPath列表中排除
JSONPath
Java代码