You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何提取点符号(dot-notation)格式的JsonPath路径列表?

JsonPath提取路径:将括号符号格式转为点符号格式

问题场景

给定JSON数据:

{"store": {"book": [{"category": "reference","author": "Nigel Rees"},{"category": "fiction","author": "Evelyn Waugh"},{"category": "fiction","author": "Herman Melville","price": 8.99},{"category": "fiction","author": "J. R. R. Tolkien"}],"bicycle": {"color": "red"}},"expensive": 10}

使用以下代码提取所有书籍作者的JsonPath路径:

Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build();
Object read = JsonPath.using(conf).parse(jsonStringRaw).read("$.store.book[*].author");

当前得到的是括号符号格式的路径:

"$['store']['book'][0]['author']"
"$['store']['book'][1]['author']"
"$['store']['book'][2]['author']"
"$['store']['book'][3]['author']"

期望得到点符号格式的路径:

$.store.book[0].author
$.store.book[1].author
$.store.book[2].author
$.store.book[3].author

解决方法

JsonPath内置了DotPathFormatter类,专门用于生成点符号格式的路径。只需在配置中指定该格式化器即可:

// 导入所需依赖类
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.DotPathFormatter;

// 修改配置,添加DotPathFormatter
Configuration conf = Configuration.builder()
    .options(Option.AS_PATH_LIST)
    .pathFormatter(new DotPathFormatter()) // 核心配置:指定点符号路径生成规则
    .build();

// 执行提取,得到点符号格式的路径列表
List<String> authorPaths = JsonPath.using(conf).parse(jsonStringRaw).read("$.store.book[*].author");

执行后即可直接生成符合预期的点符号格式路径。

内容的提问来源于stack exchange,提问作者dhvlnyk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 17:22:41