This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

** How to use Jackson to deserialize an array of objects? 支那

The documentation on Jackson data binding states that Jackson supports deseriavenues “all support for arrays” but I can’t figure out the exact syntax for this.

For a single object, I can do this:

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

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

Now, for arrays, I want to do this:

//json input
[{
    "id" : "junk",
    "stuff" : "things"
},
{
    "id" : "spam",
    "stuff" : "eggs"
}]

//Java
List<MyClass> entries = ?
Copy the code

Does anyone know if there’s a magical order? If not, what is the solution?

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

First create a mapper:

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

As an array:

MyClass[] myObjects = mapper.readValue(json, MyClass[].class);
Copy the code

As a list:

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

Another way to specify a list type:

List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
Copy the code

Answer:

From Eugene Tskhovrebov

List<MyClass> myObjects = Arrays.asList(mapper.readValue(json, MyClass[].class))
Copy the code

This solution seems to be the best for me.

The article translated from am2dgbqfb6mk75jcyanzabc67y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 6…

Author’s suggestion: Used Jackson, have a lookdoc:Arrays.asList(mapper.readValue(json, MyClass[].class))

For an extension: juejin.cn/post/696217…

Gson can support three layers of nesting, Jackson community is active, Fastjson is very popular in China, but there are shortcomings, I see ali cloud is not using Fastjson to parse JSON

The reason WHY I know ali cloud is not using Fastjson to parse JSON is because, I docking nail API document, in the source code does not appear fastjson figure, if you are interested, but PRIVATE message I want to source!


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️