如何从大型JSON响应中正确获取特定行?

发布于 2025-02-03 16:15:41 字数 2528 浏览 0 评论 0 原文

我有一个很大的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,还是可以做得更好?

i have a large json respose:

{
  "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"
      },
      
     ...

And I want to get out of there only data -> url and data -> embed_url .
How can this be done correctly?
i try add @JsonProperty to 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");
    }
}

When I try to return it from the function:

@GetMapping("/random")
    GifUrlDto getRandomGByExchangeRates();

then i get an error :

 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

Do I need to make a desiarilizer somehow, or can I do it better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2025-02-10 16:15:41

我只想离开那里的数据 - &gt; URL和数据 - &gt; Embed_url。做
我需要以某种方式做一个deSiaririzer,还是可以做得更好?

您可以在没有避难所的情况下或带有避难所的化合物进行操作。如果没有避难所,您可以使用 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 方法(如果您必须在代码中提取一次属性,则很好):

JsonNode data = mapper.readTree(json).at("/data");
String url = data.get("url").asText(); //<--url is a string
String embedUrl = data.get("embed_url").asText();//<-- embed_url is a string
GifUrlDto dto = new GifUrlDto(url, embedUrl);        

您还可以为 gifurldto 类编写一个特定的求职者,将上述代码重复使用(如果您必须对多个对象进行挑选):

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@JsonDeserialize(using = GifUrlDtoDeserializer.class)//<--added annotation
public class GifUrlDto {
    private String url;
    private String embedUrl;
}

public class GifUrlDtoDeserializer extends StdDeserializer<GifUrlDto>{

    public GifUrlDtoDeserializer() {
        super(GifUrlDto.class);
    }

    @Override
    public GifUrlDto deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
        final ObjectCodec codec = jp.getCodec();
        JsonNode root = codec.readTree(jp);
        JsonNode data = root.at("/data");
        String url = data.get("url").asText();
        String embedUrl = data.get("embed_url").asText();
        return new GifUrlDto(url, embedUrl); 
    }
}

//using the deserializer
GifUrlDto dto = mapper.readValue(json, GifUrlDto.class);

I want to get out of there only data -> url and data -> embed_url. Do
I need to make a desiarilizer somehow, or can I do it better?

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 the JsonNode#at method (good if you have to extract properties once in your code):

JsonNode data = mapper.readTree(json).at("/data");
String url = data.get("url").asText(); //<--url is a string
String embedUrl = data.get("embed_url").asText();//<-- embed_url is a string
GifUrlDto dto = new GifUrlDto(url, embedUrl);        

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):

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@JsonDeserialize(using = GifUrlDtoDeserializer.class)//<--added annotation
public class GifUrlDto {
    private String url;
    private String embedUrl;
}

public class GifUrlDtoDeserializer extends StdDeserializer<GifUrlDto>{

    public GifUrlDtoDeserializer() {
        super(GifUrlDto.class);
    }

    @Override
    public GifUrlDto deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
        final ObjectCodec codec = jp.getCodec();
        JsonNode root = codec.readTree(jp);
        JsonNode data = root.at("/data");
        String url = data.get("url").asText();
        String embedUrl = data.get("embed_url").asText();
        return new GifUrlDto(url, embedUrl); 
    }
}

//using the deserializer
GifUrlDto dto = mapper.readValue(json, GifUrlDto.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文