Home >Java >javaTutorial >How to Deserialize JSON Arrays of Objects into Java Lists using Jackson?

How to Deserialize JSON Arrays of Objects into Java Lists using Jackson?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-26 05:00:101085browse

How to Deserialize JSON Arrays of Objects into Java Lists using Jackson?

Deserializing an Array of Objects with Jackson

Question:

How to deserializing arrays of objects into a Java list using Jackson?

Solution:

  1. Create an Object Mapper:
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
  1. Deserialize as an Array:
// JSON Input: [{...}, {...}, ...]
MyClass[] myObjects = mapper.readValue(json, MyClass[].class);
  1. Deserialize as a List Using TypeReference:
List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>() {});
  1. Deserialize as a List Using Type Factory:
List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));

The above is the detailed content of How to Deserialize JSON Arrays of Objects into Java Lists using Jackson?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn