简体   繁体   中英

Deserializing field from nested objects within JSON response with Jackson

This seems like it should be a fairly solved/well-addressed issue, but I'm not finding much guidance on it -- hoping this isn't a dupe.

My scenario is basically that I am consuming paginated JSON responses that look something like this:

{ 
  "objects": [...],
  "meta": {
    "total": 5000,
    "page": 1,
    "result_pages": 20,
    "links": {
      "prev": null,
      "next": "/api/v3/objects/somequery?page=2"
    }
  }
}

Obviously this is simplified, but hopefully it gets the point across.

All I really care about are the objects and next fields, but it's looking like I'll have to create a whole hierarchy of DTOs to successfully deserialize the nested fields.

Is there a jackson annotation that would let me skip all of this? If not, is there a set of best practices for doing this that doesn't involve a host of mostly-empty classes and files?

Is there a jackson annotation that would let me skip all of this?

You can use JsonDeserialize and define custom JsonDeserializer.

class MetaDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
        JsonNode tree = jp.readValueAsTree();
        return tree.get("links").get("next").asText();
    }
}

Here I used simple map deserialization, but if you want you can implement your own fast deserialization.

And the object

class MetaObject {
    public List<Integer> objects;

    @JsonDeserialize(using = MetaDeserializer.class)
    public String meta;

    @Override
    public String toString() {
        return "MetaObject{" + "objects=" + objects + ", meta='" + meta + '\'' + '}';
    }
}

And if you invoke MetaObject result = mapper.readValue("...", MetaObject.class) you will get what you want

MetaObject{objects=[1, 2, 3], meta='/api/v3/objects/somequery?page=2'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM