2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

how to convert java object to json object using jackson api

how to convert java object to json object using jackson api
userimage

To convert a java object to json object we can follow below steps:


1st:Add maven dependency in pom.xml file:


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


2nd : Use below code to convert json object to java object:


ObjectMapper mapper = new ObjectMapper();
String jsonInString = <JSON STRING>;

//JSON from String to Object
Staff obj = mapper.readValue(jsonInString, Student.class);


The above code snippet will convert the student json string to Student object. Make sure the variable names are same in Student class and json string keys.

Answer is