如何从大型JSON响应中正确获取特定行?
我有一个很大的JSON响应:
{
"data" : {
"type" : "gif",
"id" : "BM10Y3Qq3EwK3z8yQd",
"url" : "https://giphy.com/gifs/ramseysolutions-money-gone-BM10Y3Qq3EwK3z8yQd",
"slug" : "ramseysolutions-money-gone-BM10Y3Qq3EwK3z8yQd",
"bitly_gif_url" : "https://gph.is/2mc28WP",
"bitly_url" : "https://gph.is/2mc28WP",
"embed_url" : "https://giphy.com/embed/BM10Y3Qq3EwK3z8yQd",
"username" : "ramseysolutions",
"source" : "",
"title" : "rachel cruze money GIF by Ramsey Solutions",
"rating" : "g",
"content_url" : "",
"source_tld" : "",
"source_post_url" : "",
"is_sticker" : 0,
"import_datetime" : "2018-07-12 12:40:11",
"trending_datetime" : "0000-00-00 00:00:00",
"images" : {
"fixed_width_still" : {
"height" : "113",
"size" : "14977",
"url" : "https://media4.giphy.com/media/BM10Y3Qq3EwK3z8yQd/200w_s.gif?cid=294ee95f49badf517d33e5cd874941ff542e5d2559cf692c&rid=200w_s.gif&ct=g",
"width" : "200"
},
"preview_gif" : {
"height" : "99",
"size" : "48529",
"url" : "https://media4.giphy.com/media/BM10Y3Qq3EwK3z8yQd/giphy-preview.gif?cid=294ee95f49badf517d33e5cd874941ff542e5d2559cf692c&rid=giphy-preview.gif&ct=g",
"width" : "176"
},
...
我想离开那里只能 Data -> URL
和数据 - > Embed_url
。
如何正确完成?
我尝试将 @jsonproperty添加到setter
:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class GifUrlDto {
private String url;
private String embedUrl;
@JsonProperty("data")
public void setUrl(Map<String, String> data) {
this.url =data.get("url");
this.embedUrl = data.get("embed_url");
}
}
当我尝试从函数中返回它时:
@GetMapping("/random")
GifUrlDto getRandomGByExchangeRates();
然后我得到一个错误:
Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 665] (through reference chain: com.alfa.bank.project.gifAndExchangeRate.dto.GifUrlDto["data"]->java.util.LinkedHashMap["images"])] with root cause
我需要以某种方式做一个deSiarilizer,还是可以做得更好?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在没有避难所的情况下或带有避难所的化合物进行操作。如果没有避难所,您可以使用 jsonnode 对象,将其转换为
jsonnode
对象,可以将其转换为jsonnode
对象,您可以将其转换为json文件的一部分javadoc/2.12/com/fasterxml/jackson/databind/jsonnode.html#at-com.fasterxml.jackson.core.core.jsonpointer-“ rel =“ nofollow noreferrer”>
jsonnode#at
方法(如果您必须在代码中提取一次属性,则很好):
您还可以为
gifurldto
类编写一个特定的求职者,将上述代码重复使用(如果您必须对多个对象进行挑选):You can do it either without a deserializer or with a deserializer. Without a deserializer you can isolate the part of the json file you are interested and convert it to a
JsonNode
object using theJsonNode#at
method (good if you have to extract properties once in your code):You can also write a specific deserializer for the
GifUrlDto
class reusing the above code (good if you have to deserialize more than one object):