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,050 points
547 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,880 points

Related Questions In Java

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 13, 2018 in Java by samarth295
• 2,190 points
120 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.o ...READ MORE

answered Aug 16 in Java by Sirajul
• 39,140 points
259 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,350 points
210 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19 in Java by Sirajul
• 39,140 points
192 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,190 points
405 views
+1 vote
10 answers

Iterate over a JSONObject?

You can use the following method to ...READ MORE

answered Dec 7, 2018 in Java by Shuvodip
19,225 views
0 votes
1 answer

Escaping strings in JSON

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

answered Oct 3, 2018 in Java by geek.erkami
• 2,640 points
680 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 in Java by FooBayo
• 120 points

edited Jun 19 by FooBayo 109 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,880 points
108 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,880 points
69 views