Jackson – Convert JSON array string to List
Few Jackson examples to convert a JSON array string to a List
// JSON array string
// [{"name":"mkyong", "age":37}, {"name":"fong", "age":38}]
ObjectMapper mapper = new ObjectMapper();
String json = "[{\"name\":\"mkyong\", \"age\":37}, {\"name\":\"fong\", \"age\":38}]";
// 1. convert JSON array to Array objects
Person[] pp1 = mapper.readValue(json, Person[].class);
// 2. convert JSON array to List of objects
List<Person> ppl2 = Arrays.asList(mapper.readValue(json, Person[].class));
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
P.S Tested with Jackson 2.9.8
1. Convert JSON array string to List
1.1 JSON array string
[{"name":"mkyong", "age":37}, {"name":"fong", "age":38}]
1.2 Create an object to map the above JSON fields.
package com.mkyong;
public class Person {
String name;
Integer age;
//getters and setters
}
1.3 Convert the JSON array string to a List
JacksonArrayExample.java
package com.mkyong;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class JacksonArrayExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String json = "[{\"name\":\"mkyong\", \"age\":37}, {\"name\":\"fong\", \"age\":38}]";
try {
// 1. convert JSON array to Array objects
Person[] pp1 = mapper.readValue(json, Person[].class);
System.out.println("JSON array to Array objects...");
for (Person person : pp1) {
System.out.println(person);
}
// 2. convert JSON array to List of objects
List<Person> ppl2 = Arrays.asList(mapper.readValue(json, Person[].class));
System.out.println("\nJSON array to List of objects");
ppl2.stream().forEach(x -> System.out.println(x));
// 3. alternative
List<Person> pp3 = mapper.readValue(json, new TypeReference<List<Person>>() {});
System.out.println("\nAlternative...");
pp3.stream().forEach(x -> System.out.println(x));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
1. JSON array to Array objects...
Person{name='mkyong', age=37}
Person{name='fong', age=38}
2. JSON array to List of objects
Person{name='mkyong', age=37}
Person{name='fong', age=38}
3. Alternative...
Person{name='mkyong', age=37}
Person{name='fong', age=38}
Hi, when i do you cod, System.out.println(person); return null…
[{“name”:”Test”},{“name”:”Test2″}], two line: null, which means that is not read names
How to map a json which has more key/values with an array, like below? I create a different class for members and jsonmapper fails with
UnrecognizedPropertyException
because the DTO has a variable Member and not userId, email, etc{
“messageId”: “8335c1b0-ce49-4cc1-acaf-a324959b3f47”,
“members” :
[
{“userId”:”00001305-afa2-4e3c-8774-f5d664dacf1f”,
“email”:”[email protected]”,
“locale” : “en-US”
},
{“userId”: “00001305-afa2-4e3c-8774-f5d664dacf1g”,
“email”: “[email protected]”,
“locale” : “en-US”
}
],
“timestamp” : “1585631867”
}
https://stackoverflow.com/questions/4486787/jackson-with-json-unrecognized-field-not-marked-as-ignorable
Hi! I need to do this, but with more complex oject with at least 3 levels of attibutes. But I dont ned allthe data from the childs or the main object. I`m getting UnrecognizedPropertyException for this prperties I dont want and havent created in my pojo. Thhe exception sugests to mark it as ignorable…. How can I do this, and is there a way much simplier to do this? Like just mapping the fields I have on my pojo? Thanks!
I’ve found this :
Objectmapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
How to return json Object like {“Person” : [{“name”:”mkyong”, “age”:37}, {“name”:”fong”, “age”:38}]}
public JSONObject convertList(List items) throws JSONException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode listNode = objectMapper.valueToTree(items);
JSONArray response = new JSONArray(listNode.toString());
JSONObject jsonObject = new JSONObject();
return jsonObject.put(“Person”, request);
}