How to parse JSON Array with Jackson
This article uses the Jackson framework to parse JSON Array in Java.
Table of contents:
- 1. Download Jackson
- 2. Parse JSON Array with Jackson
- 3. Convert List of Objects to JSON Array
- 4. Write JSON Array with a list label
- 5. Parse JSON Array contains a list label
- 6. Download Source Code
- 7. References
P.S Tested with Jackson 2.17.0
1. Download Jackson
Simply declare jackson-databind
in the pom.xml
, and it will automatically pull in jackson-annotations
, jackson-core
, and other necessary dependencies.
pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
2. Parse JSON Array with Jackson
Below is a JSON array string.
[
{
"name" : "mkyong",
"age" : 42
},
{
"name" : "ah pig",
"age" : 20
}
]
Create a Person
object that matches the JSON property name.
Person.java
package com.mkyong.json.model;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// getters, setters, toString()
}
In Jackson, we can convert the JSON array to an Array
or List
.
JsonArrayToObjectExample.java
package com.mkyong.json.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.Person;
import java.util.List;
public class JsonArrayToObjectExample {
public static void main(String[] args) throws JsonProcessingException {
String jsonArray = "[{\"name\":\"mkyong\", \"age\":42}, {\"name\":\"ah pig\", \"age\":20}]";
ObjectMapper mapper = new ObjectMapper();
// 1. convert JSON array to Array objects
Person[] person1 = mapper.readValue(jsonArray, Person[].class);
for (Person p : person1) {
System.out.println(p);
}
// 2. convert JSON array to List
List<Person> person2 = mapper.readValue(jsonArray, new TypeReference<>() {
});
person2.forEach(System.out::println);
}
}
output
Person{name='mkyong', age=42}
Person{name='ah pig', age=20}
Person{name='mkyong', age=42}
Person{name='ah pig', age=20}
3. Convert List of Objects to JSON Array
JsonArrayToObjectExample2.java
package com.mkyong.json.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.Person;
import java.util.Arrays;
import java.util.List;
public class JsonArrayToObjectExample2 {
public static void main(String[] args) throws JsonProcessingException {
List<Person> list = Arrays.asList(
new Person("mkyong", 42),
new Person("ah pig", 20)
);
ObjectMapper mapper = new ObjectMapper();
String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);
System.out.println(result);
}
}
Output
[ {
"name" : "mkyong",
"age" : 42
}, {
"name" : "ah pig",
"age" : 20
} ]
4. Write JSON Array with a list label
This example converts a list of Person
objects to a JSON string with the list labeled as "Person":
JsonArrayToObjectExample3.java
package com.mkyong.json.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.Person;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JsonArrayToObjectExample3 {
public static void main(String[] args) throws JsonProcessingException {
// Create a list of Person objects
List<Person> people = Arrays.asList(
new Person("mkyong", 42),
new Person("ah pig", 20)
);
// Wrap the list in a Map with "Person" as the key
Map<String, List<Person>> wrapper = new HashMap<>();
wrapper.put("Person", people);
// Create an ObjectMapper instance and convert the map to JSON
ObjectMapper mapper = new ObjectMapper();
String jsonOutput = mapper.writeValueAsString(wrapper);
// Print the JSON output
// {"Person":[{"name":"mkyong","age":42},{"name":"ah pig","age":20}]}
System.out.println(jsonOutput);
}
}
Output
{"Person":[{"name":"mkyong","age":42},{"name":"ah pig","age":20}]}
5. Parse JSON Array contains a list label
The technique is the same with the above example 4: first converts the JSON string to Map<String, List<Person>>
and manually get the List<Person>
later.
{
"Person" : [
{
"name" : "mkyong",
"age" : 42
},
{
"name" : "ah pig",
"age" : 20
}
]
}
JsonArrayToObjectExample4.java
package com.mkyong.json.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.Person;
import java.util.List;
import java.util.Map;
public class JsonArrayToObjectExample4 {
public static void main(String[] args) throws JsonProcessingException {
String jsonInput = "{\"Person\":[{\"name\":\"mkyong\",\"age\":42},{\"name\":\"ah pig\",\"age\":20}]}";
// Create ObjectMapper instance
ObjectMapper mapper = new ObjectMapper();
// Convert JSON string to Map<String, List<Person>>
Map<String, List<Person>> result = mapper.readValue(jsonInput, new TypeReference<>() {
});
// Retrieve the list of persons
List<Person> persons = result.get("Person");
// Print the parsed persons
for (Person person : persons) {
System.out.println("Name: " + person.getName() + ", Age: " + person.getAge());
}
}
}
6. Download Source Code
$ git clone https://github.com/mkyong/java-json
7. References
About Author
Comments
Subscribe
0 Comments