gpt4 book ai didi

java - JsonPath - 缓存 JSON 然后执行多个查询?

转载 作者:行者123 更新时间:2023-11-29 04:22:14 25 4
gpt4 key购买 nike

我继承了一些针对单个 JSON 文件执行大量 JsonPath 查询的代码。对于每个查询,它都会传入 JSON 文件和查询。然后这将成为对 JsonPath.read(jsonFile, jpathQuery) 的调用。

有没有办法避免它构建 JSON 执行查询所需的任何内部结构?有没有办法让它构建一次 JSON 的内部模型,然后对其执行多个查询?

谢谢 - 戴夫

最佳答案

我假设您使用的是 this jsonpath library .

在他们的文档中有这个

The simplest most straight forward way to use JsonPath is via the static read API.

String json = "...";
List<String> authors = JsonPath.read(json, "$.store.book[*].author");

If you only want to read once this is OK. In case you need to read an other path as well this is not the way to go since the document will be parsed every time you call JsonPath.read(...). To avoid the problem you can parse the json first.

String json = "..."; Object document =
Configuration.defaultConfiguration().jsonProvider().parse(json);
String author0 = JsonPath.read(document, "$.store.book[0].author");
String author1 = JsonPath.read(document, "$.store.book[1].author");

关于java - JsonPath - 缓存 JSON 然后执行多个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48392367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com