Convert JSON to Java Object

Convert JSON to Java Object

Here’s an example on how you can convert json to java object. We start first by converting to simple POJO then we’ll also show you how you can do the same for Collections Object.

Add Jackson Dependency

Add the dependency for JSON.


    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    2.7.4


    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    2.7.4

Converting JSON to simple POJO

For example, we have this JSON:

{  
   "id":"60"
   "codeId":"1",
   "codeName":"IT",
   "deleteFlag":0,
   "sort":"1",
}

and we need this JSON to be converted to MstCode object:

public class MstCode {

    private int id;
    private int codeId;
    private String codeName;
    private int deleteFlag;
    private int sort;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCodeId() {
        return codeId;
    }

    public void setCodeId(int codeId) {
        this.codeId = codeId;
    }

    public String getCodeName() {
        return codeName;
    }

    public void setCodeName(String codeName) {
        this.codeName = codeName;
    }

    public int getDeleteFlag() {
        return deleteFlag;
    }

    public void setDeleteFlag(int deleteFlag) {
        this.deleteFlag = deleteFlag;
    }

    public int getSort() {
        return sort;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }
}


In order for us to convert json to java object, we will use jackson ObjectMapper class.

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonConverter {

    public static void main(String[] args) {
        String json = "{\"deleteFlag\":0,\"codeId\":\"1\",\"codeName\":\"IT\",\"sort\":\"1\",\"id\":\"60\"}";
        ObjectMapper mapper = new ObjectMapper();
        try {
            MstCode mstCode = mapper.readValue(json, MstCode.class);
            System.out.print(mstCode.getCodeName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This produces the below output:

IT

Converting JSON to List of Objects

Now that we have learned how to convert json to POJO, we also should now how we can convert a json which consist of array of json to java.util.List.
For example, consider the below JSON:

[  
   {  
      "deleteFlag":0,
      "codeId":"1",
      "codeName":"IT",
      "sort":"1",
      "id":"60"
   },
   {  
      "deleteFlag":0,
      "codeId":"2",
      "codeName":"Accounting",
      "sort":"2",
      "id":"61"
   },
   {  
      "deleteFlag":0,
      "codeId":"3",
      "codeName":"Support",
      "sort":"3",
      "id":"62"
   }
]

The above json consist of three json which has named IT, Accounting, and Support. To be able to parsed this to java List, we’ll also use the same method but we will changed the second argument.

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

public class JsonConverter {

    public static void main(String[] args) {
        String json = "[{\"deleteFlag\":0,\"codeId\":\"1\",\"codeName\":\"IT\",\"sort\":\"1\",\"id\":\"60\"},{\"deleteFlag\":0,\"codeId\":\"2\",\"codeName\":\"Accounting\",\"sort\":\"2\",\"id\":\"61\"},{\"deleteFlag\":0,\"codeId\":\"3\",\"codeName\":\"Support\",\"sort\":\"3\",\"id\":\"62\"}]";
        ObjectMapper mapper = new ObjectMapper();
        try {
            List<MstCode> mstCodes = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, MstCode.class));
            System.out.println(mstCodes.size());
            System.out.println(mstCodes.get(0).getCodeName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

To convert json to List of object, we will construct the collection type:

mapper.getTypeFactory().constructCollectionType(List.class, MstCode.class)

where List.class is the Collection class and MstCode.class is the class of object that your list will be holding.
Running the above code will result to the following:

3
IT
Share this tutorial!