Nelson
Nelson

Reputation: 3232

How to use Jackson to deserialize an array of objects in side of an object?

How can I deserialize only the items array?

{
 "code": 200,
 "bookmarks": {
   "count": 2,
   "items": [
     {
       "status": "I",
       "id": "1",
       "percent": 9,
       "timestamp": 1462826317475
     },
     {
       "status": "I",
       "id": "2",
       "percent": 10,
       "timestamp": 1462909994981
     }
   ],
   "links": []
  }
}

I know for an array I can do something like this:

List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});

Can I specify only deserializing items?

Upvotes: 0

Views: 93

Answers (1)

StaxMan
StaxMan

Reputation: 116472

Use ObjectReaders at(JsonPointer) method to specify sub-tree to bind. Something like:

Item[] items = mapper.readerFor(Item[].class).at("/bookmarks/items").readValue();

Upvotes: 1

Related Questions