Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert JSON to Java Object

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 330
    Comment on it

    How to convert JSON to a Java object: In many application we use web services and those web services deals with the JSON object, so when those web services need to communicate with our web application we need to convert those JSON objects to Java objects. JSON object can be sent as simple text format or can be as a JSON document using web service.

    So the question is how to convert JSON object to Java object ? The answer of this question is simple use Jackson api to process the JSON objects. Jackson provides easy way to process the JSON object and can convert the JSON to Java object.

    To use Jackson api in our project we need to download the following jars :

    1. jackson-databind-2.2.3.jar
    2. jackson-core-2.2.3.jar
    3. jackson-annotations-2.2.3.jar

    Or we can use maven dependency in our project, put following code in pom.xml :

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

    In our example we are converting JSON to Java Object. Here we are using simple JSON document, and using this we will map that JSON object to Java object.

    Example to convert JSON to a Java object :

    Employee.java
    package com.evon.jakson.conversion;
    
    public class Employee {
        private long id;
        private String name;
        private String occupation;
        Employee(){
        }
        Employee(long id, String name, String occupation){
            this.id=id;
            this.name=name;
            this.occupation=occupation;
        }
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getOccupation() {
            return occupation;
        }
        public void setOccupation(String occupation) {
            this.occupation = occupation;
        }
        @Override
        public String toString(){
            return getId() + ", "+getName()+", "+getOccupation();
        }
    }
    

    Employee.java is a model object which need to map to the JSON object. Now we will write the code to convert that take the JSON document and map the JSON object to Employee object.

    JsonToJavaObject.java

    package com.evon.jakson.conversion;
    import java.io.File;
    import java.io.IOException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class JsonToJavaObject {
        public static void main(String[] args) {
            String filePath = "/home/sumit/employee.json";
            Employee employee = null;
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                employee = objectMapper.readValue(new File(filePath),
                        Employee.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
    System.out.println(Employee Details );
            System.out.println(employee);
        }
    }
    

    In the above code we are passing a file called employee.json which is having the JSON object and ObjectMapper is a class file of Jackon's api which map the JSON object to the Java object employee.

    Output :

    Employee Details 
    1, Sumit, Software Engineer
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: