JsonPath - 缓存 JSON 然后执行多个查询?
JsonPath - cache the JSON and then perform multiple queries?
我继承了一些针对单个 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");
我继承了一些针对单个 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");