0

For example, I have a JSON file named simple.json which has the below code.

[
  { "name" : "David", "age" : 45 },
  { "name" : "Norma", "age" : 44 }
]

and a class name Record.java

public class Record{
   private String name;
   private int age;

   // corresponding constructor and getters functions

}

How do I de-serialize simple.json file into a list of record objects?

Maybe the list looks somewhat like ArrayList<Record> recordList;

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Debayan
  • 459
  • 4
  • 6

3 Answers3

2

You can use ObjectMapper for this:

ObjectMapper objectMapper = new ObjectMapper();

String jsonRecordArray =
        "[{ \"name\" : \"David\", \"age\" : 45 },\n" +
        " { \"name\" : \"Norma\", \"age\" : 44 }\n" +
        "]";
List<Record> listRecord = objectMapper.readValue(jsonRecordArray, new TypeReference<>() {
});

In maven the relevant dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.2.2</version>
</dependency>

Remember to handle JsonProcessingException, for example by adding it to the method signature.

jwpol
  • 1,188
  • 10
  • 22
1

First of all the answer from jwpol is correct and could be used as a solution. Just a bit of additional clarifications: The library that @jwpol suggests is called JSON-Jackson or also known as faster XML. Here is the main site for it and there are many others. This library is de-facto is JSON standard (used by default in Spring Boot). One popular alternative would be Gson - a Google implementation of JSON library. Here are 2 sites to read about it: https://github.com/google/gson, https://sites.google.com/site/gson/gson-user-guide.

Also, for simplistic use where you just need simple serialization/de-serialization capabilities I wrote my own simple feature that is a thin wrapper over Jackson Json library that gives you simplified use without directly importing Jackson library and configuring it. With my feature your code could look like

    String jsonRecordArray =
            "[{ \"name\" : \"David\", \"age\" : 45 },\n" +
            " { \"name\" : \"Norma\", \"age\" : 44 }\n" +
            "]";
List<Record> listRecord;
    try {
      listRecord = JsonUtils.readObjectFromJsonString(jsonRecordArray, List.class);
    } catch(IOException ioe) {
    ...
    }

BTW, in jwpol exaple exception handling is missing.

If you want to use my feature you need to import MgntUtils open source library written by me. You can get Maven artifacts here and or get it on Github (including source code and Javadoc). Here is Javadoc for JsonUtils class

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

The below code served my purpose.

Gson gson = new Gson();
JsonReader reader = new JsonReader("PATH_OF_THE_JSON_FILE");
Record[] recordList = gson.fromJson(reader, Record[].class);

for(Record record : recordList){
// Here I could access name and age of each
// record by record.getName() and record.getAge()
 }

Debayan
  • 459
  • 4
  • 6