ObjectMapper map to List of objects

  • Given a list of user defined objects, we would liketo convert list of pojo objectsto JSON [and JSON to list of objects].
  • We will use the jacksons objectmapper, to serialize list of objectsto JSON &deserialize JSON to List of objects.
  • We will create Person class &we will perform following operations withPerson class.
    • Convert List of Person objects to JSON
    • Convert the JSONto List of Person objects.
  • We have already discussed [similar transformations]:
    • Convert Objects to/from JSON [jackson]
    • Convert map to/from JSON [jackson]

1. Jackson objectMapper maven dependencies

com.fasterxml.jackson.core jackson-databind 2.7.1

2. Convert list of objects to/from JSON in java [jackson objectmapper]

2.1. Person Class:

  • Person class containing attributesis as follows:
package org.learn; public class Person { public String firstName; public String lastName; public int age; public Person[] { } public Person[String firstName, String lastName, int age] { this.firstName = firstName; this.lastName = lastName; this.age = age; } public String toString[] { return "[" + firstName + " " + lastName + " " + age +"]"; } }

2.2. JSONListConverter Class:

JSONListConverter class is responsible for performing following operations.

  1. Convert List of Person objects to JSON String in java.
  2. Convert JSONString to List of Person objects in java.
package org.learn; import java.io.IOException; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class JSONListConverter { public static void main[ String[] args ] throws IOException { ObjectMapper objectMapper = new ObjectMapper[]; //Set pretty printing of json objectMapper.enable[SerializationFeature.INDENT_OUTPUT]; //Define map which will be converted to JSON List personList = Stream.of[ new Person["Mike", "harvey", 34], new Person["Nick", "young", 75], new Person["Jack", "slater", 21 ], new Person["gary", "hudson", 55]] .collect[Collectors.toList[]]; //1. Convert List of Person objects to JSON String arrayToJson = objectMapper.writeValueAsString[personList]; System.out.println["1. Convert List of person objects to JSON :"]; System.out.println[arrayToJson]; //2. Convert JSON to List of Person objects //Define Custom Type reference for List type TypeReference mapType = new TypeReference[] {}; List jsonToPersonList = objectMapper.readValue[arrayToJson, mapType]; System.out.println["\n2. Convert JSON to List of person objects :"]; //Print list of person objects output using Java 8 jsonToPersonList.forEach[System.out::println]; } }

Download Example Code Jackson List Object to JSON

3. Output list of objects to/from JSON in java [jackson objectmapper]

1. Convert List of person objects to JSON : [ { "firstName" : "Mike", "lastName" : "harvey", "age" : 34 }, { "firstName" : "Nick", "lastName" : "young", "age" : 75 }, { "firstName" : "Jack", "lastName" : "slater", "age" : 21 }, { "firstName" : "gary", "lastName" : "hudson", "age" : 55 } ] 2. Convert JSON to List of person objects : [Mike harvey 34] [Nick young 75] [Jack slater 21] [gary hudson 55]

You may also like:

  1. Convert Map to/from JSON string in java [jackson objectmapper/ example]
  2. Convert Array of Objects to / from JSON Jackson ObjectMapper
  3. Convert object to/from json string in java [jackson objectmapper-example]
  4. Convert object having date to/from JSON in java [Jackson ObjectMapper example]
  5. Date serialization & deserialization POJO to JSON [GSON & example]

Video liên quan

Chủ Đề