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
Release: 2024-12-26 05:00:10
Original
980 people have browsed it

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();
Copy after login
  1. Deserialize as an Array:
// JSON Input: [{...}, {...}, ...]
MyClass[] myObjects = mapper.readValue(json, MyClass[].class);
Copy after login
  1. Deserialize as a List Using TypeReference:
List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>() {});
Copy after login
  1. Deserialize as a List Using Type Factory:
List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template