본문 바로가기

Tip

JSON Array String을 list로 convert 변환

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>

 

Convert JSON array string to List

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(); }