JAVA JSON and JSON JAVA conversion with Jackson

JAVA JSON and JSON JAVA conversion with Jackson

Jackson library is a suite of data processing tools that includes JSON parsing and conversion library as well. Java JSON and json java conversion with Jackson is very easy and straight forward process where you need to create object of ObjectMapper class and user method writeValueAsString to convert Java to JSON and readValue to convert JSON to java object.

Below three Jackson jar need to be in your class path beforehand:

  • jackson-annotations-2.2.3 (or latest version)
  • jackson-core-2.2.3 (or latest version)
  • jackson-databind-2.2.3 (or latest version)

You could also directly download it form Jackson site here or we have included all three jar in the bottom for download.

Below is sample java class which shows how to do it:

package com.javahonk;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JavaJSONWithJackson {

    public static void main(String[] args) {

    try {
        List<Employee> employeeList = new ArrayList<Employee>();
        for (int i = 0; i < 2; i++) {
        Employee employee = new Employee();
        employee.setName("John Landy");
        employee.setPosition("System Architect");
        employee.setOffice("NY");
        employeeList.add(employee);
        }

        // Java to JSON conversion
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(employeeList);
        System.out.println("Java to JSON:\n");
        System.out.println(json);

        // JSON to java conversion
        System.out.println("\nJSON to java conversion\n");
        Employee[] employee = mapper.readValue(json, 
            Employee[].class);
        System.out.println("Size of JSON object:" 
                + employee.length + "\n");
        for (Employee employee2 : employee) {
        System.out.println("Name: " + employee2.getName() 
            + " Position: "+ employee2.getPosition() 
            + " Office: " + employee2.getOffice());
        }

    } catch (JsonProcessingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

}

class Employee {

    private String name;
    private String position;
    private String office;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getPosition() {
    return position;
    }

    public void setPosition(String position) {
    this.position = position;
    }

    public String getOffice() {
    return office;
    }

    public void setOffice(String office) {
    this.office = office;
    }

}

 

Output:

JAVA JSON and JSON JAVA conversion with Jackson

download  Download Jackson Jar

Leave a Reply

Your email address will not be published. Required fields are marked *