gpt4 book ai didi

java - 控制 JsonPath 的 read() 方法返回的数据类型

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:00 25 4
gpt4 key购买 nike

我希望 JsonPath 始终将其解析为字符串的数据返回。但是,JsonPath 的 read() 方法的返回类型会根据所解析数据的类型而有所不同,因为 JsonPath 总是会猜测数据类型并返回该类型的结果。

问题是我无法知道将要返回哪种数据,因为我只能访问一组需要输入到 read() 函数中的路径。这意味着我必须将返回的 JsonPath 数据存储在一个对象数组中,并使用包含 instanceof 关键字的非常难看的 if/else-if 语句集合来确定数据是什么(然后将其转换为适当的类型)。

有人知道强制 JsonPath 返回字符串的方法吗?或者其他人可以想出更好的解决方法吗?

这是我现在正在做的事情(用 Java):

ArrayList<Object> realData = JsonPath.read(dataDumpFile, current.getPath());

我想做的事情:

ArrayList<String> realData = JsonPath.read(dataDumpFile, current.getPath());

docs我发现了这个:

When using JsonPath in java its important to know what type you expect in your result. JsonPath will automatically try to cast the result to the type expected by the invoker.

//Will throw an java.lang.ClassCastException    
List<String> list = JsonPath.parse(json).read("$.store.book[0].author")

//Works fine
String author = JsonPath.parse(json).read("$.store.book[0].author")

“调用者期望的类型”是什么意思?

最佳答案

在 JsonPath 的典型用例中,库假定 we know what to expect from a path :

  • 路径确定吗?一个路径是:

    • 确定是否只能有一个结果,例如$..book[0]第一本书。
    • 不确定是否可能有多个结果,例如$..book[?(@.isbn)]对于具有某些属性的所有书籍;返回类型可以是列表。
  • 结果应该是什么特定类型?例如,您可以期望返回类型 DateList<Date>而不是 ObjectList<Object> .

在您的情况下,以上所有内容都不重要,因为您事先不知道类型,您只需要 JSON 字符串形式的结果。

JsonPath 中的默认解析器太聪明了,会将所有内容转换为漂亮的 LinkedHashMap 对象;但如果你使用 JacksonJsonNodeJsonProvider , 你会得到 JsonNode 的结果对象,而你是一个toString()调用 JSON 字符串结果。

更好的是,您可以使用 JsonPath option ALWAYS_RETURN_LIST ,这意味着您无需担心您的路径是确定的还是不确定的(无论您的结果是单个对象还是列表)。

// Example from https://github.com/jayway/JsonPath#path-examples
final String json = "{\"store\": {\"book\": [{\"category\": \"reference\",\"author\": \"Nigel Rees\",\"title\": \"Sayings of the Century\",\"price\": 8.95},{\"category\": \"fiction\",\"author\": \"Evelyn Waugh\",\"title\": \"Sword of Honour\",\"price\": 12.99},{\"category\": \"fiction\",\"author\": \"Herman Melville\",\"title\": \"Moby Dick\",\"isbn\": \"0-553-21311-3\",\"price\": 8.99},{\"category\": \"fiction\",\"author\": \"J. R. R. Tolkien\",\"title\": \"The Lord of the Rings\",\"isbn\": \"0-395-19395-8\",\"price\": 22.99}],\"bicycle\": {\"color\": \"red\",\"price\": 19.95}},\"expensive\": 10}";

Configuration conf = Configuration.builder().jsonProvider(new JacksonJsonNodeJsonProvider())
.options(Option.ALWAYS_RETURN_LIST, Option.SUPPRESS_EXCEPTIONS).build();

ArrayNode node = JsonPath.using(conf).parse(json).read("$.store.book[*]"); // indefinite
for (Object o : node) {
System.out.println(o.toString());
}
// {"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95}
// {"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}
// {"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}
// {"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}

node = JsonPath.using(conf).parse(json).read("$.store.book[0].author"); // definite
for (Object o : node) {
System.out.println(o.toString());
}
// "Nigel Rees"

关于java - 控制 JsonPath 的 read() 方法返回的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40748442/

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