gpt4 book ai didi

java - 使用 Jackson 读取 JSON 字符串的一部分

转载 作者:IT老高 更新时间:2023-10-28 12:55:09 26 4
gpt4 key购买 nike

JSON字符串如下

{
"rank":"-text_relevance",
"match-expr":"(label 'star wars')",
"hits":{
"found":7,
"start":0,
"hit":[
{"id":"tt1185834",
"data":{
"actor":["Abercrombie, Ian","Baker, Dee","Burton, Corey"],
"title":["Star Wars: The Clone Wars"]
}
},
.
.
.
{"id":"tt0121766",
"data":{
"actor":["Bai, Ling","Bryant, Gene","Castle-Hughes, Keisha"],
"title":["Star Wars: Episode III - Revenge of the Sith"]
}
}
]
},
"info":{
"rid":"b7c167f6c2da6d93531b9a7b314ad030b3a74803b4b7797edb905ba5a6a08",
"time-ms":2,
"cpu-time-ms":0
}
}

它有很多字段,但我只想要数据字段。这行不通:

mapper.readvalue(jsonString,Data.class); 

如何让 Jackson 只读取“数据”字段?

最佳答案

Jackson 2.3 现在有一个可以使用的 JsonPointer 类。在他们的quick overview 中有一个简单的例子。为发布。

Usage is simple: for JSON like

{
"address" : { "street" : "2940 5th Ave", "zip" : 980021 },
"dimensions" : [ 10.0, 20.0, 15.0 ]
}

you could use expressions like:

  JsonNode root = mapper.readTree(src); 
int zip =root.at("/address/zip").asIntValue();
double height = root.add("/dimensions/1").asDoubleValue();// assuming it's the second number in there

关于java - 使用 Jackson 读取 JSON 字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11677395/

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