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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 269
    Comment on it

    How to convert Java object to JSON : Converting Java object to JSON is a little complicated work in Java. We can use the third party api to make this easy.
    Jackson is one of the most popular api for JAVA JSON processing. Jackson api is fast, convenient and high performance api for json processing. We can convert Java object to json object and vice versa. To use Jackson api in our project we need to download the 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>
    

    Or can download following jar and put in project's lib :

    jackson-databind-2.2.3.jar 
    jackson-core-2.2.3.jar 
    jackson-annotations-2.2.3.jar
    

    In our example we are converting Java Object to JSON object. Here we are using simple objects, you can use complex objects too to convert Object to JSON.

    Example of converting Java Object to JSON :

    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 we will convert to JSON object. Now we will write the code to convert that Employee object to JSON document file.

    JavaObjectToJSON.java

    package com.evon.jakson.conversion;
    import java.io.File;
    import java.io.IOException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class JavaObjectToJSON {
        public static void main(String []args){
            String filePath = "/home/sumit/employee.json";
            Employee crocodile = new Employee(1, "Sumit", "Software Engineer");
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                File file = new File(filePath);
                if(!file.exists())file.createNewFile();
                objectMapper.writeValue(new File(filePath), crocodile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    The above code will create a employee.json file and save the employee object to this document as a json object.Following is the view how the file will look like :

    Output File : employee.json

    {"id":1,"name":"Sumit","occupation":"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: