Using Jackson to deserialise an array of object

0 votes

For a single object I would do this:

//json input
{
    "id" : "junk",
    "stuff" : "bread"
}

//Java
MyClass instance = objectMapper.readValue(json, MyClass.class);


Now for an array I want to do this:

//json input
[{
    "id" : "junk",
    "stuff" : "bread"
},
{
    "id" : "healthy",
    "stuff" : "eggs"
}]

//Java
List<MyClass> entries = ?


Can anyone help me to find out the missing command?

Sep 26, 2018 in Java by Daisy
• 8,140 points
5,315 views

1 answer to this question.

0 votes

First create a mapper :

import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3
ObjectMapper mapper = new ObjectMapper();

As Array:

MyClass[] myObjects = mapper.readValue(json, MyClass[].class);
As List:
List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});
Another way to specify the List type:
List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
answered Sep 26, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
0 answers

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

I'd want to transform the following object: {"1":5,"2":7,"3":0,"4" ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
1,126 views
0 votes
2 answers

How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or  yourList.stream().forEach(a -> ...READ MORE

answered Aug 14, 2018 in Java by samarth295
• 2,220 points
3,034 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
2,398 views
0 votes
1 answer

How to convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) { ...READ MORE

answered Sep 26, 2018 in Java by sharth
• 3,370 points
2,130 views
0 votes
1 answer

What is the simplest way to read JSON from a URL in java

Read json from url use url.openStream() and read contents ...READ MORE

answered Jun 13, 2018 in Java by samarth295
• 2,220 points
5,178 views
+1 vote
12 answers

Iterate over a JSONObject?

Assuming your JSON object is saved in ...READ MORE

answered Dec 11, 2020 in Java by Rajiv
• 8,870 points
124,764 views
0 votes
1 answer

Escaping strings in JSON

You can find a JSON library in your ...READ MORE

answered Oct 4, 2018 in Java by geek.erkami
• 2,680 points
24,417 views
0 votes
0 answers

Sending request using json object return 404 error code

I am getting mad with this code ...READ MORE

Jun 19, 2019 in Java by FooBayo
• 120 points

edited Jun 19, 2019 by FooBayo 2,933 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,920 points
5,656 views
0 votes
2 answers

How to test that an array contains a certain value?

public static final String[] VALUES = new ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,920 points
1,103 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP