Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert POJO to JSON and vice versa?

Tags:

java

json

pojo

I want to know if there is any Java API available to convert a POJO object to a JSON object and vice versa.

like image 988
Mariah Avatar asked Mar 06 '12 23:03

Mariah


People also ask

How does spring boot convert object to JSON?

When Jackson is on the classpath an ObjectMapper bean is automatically configured. The spring-boot-starter-json is pulled with the spring-boot-starter-web . In Spring objects are automatically convered to JSON with the Jackson library. Spring can be configured to convert to XML as well.


2 Answers

Take a look at https://www.json.org

Imagine that you have a simple Java class like this:

public class Person {      private String name;     private Integer age;      public String getName() { return this.name; }     public void setName( String name ) { this.name = name; }      public Integer getAge() { return this.age; }     public void setAge( Integer age ) { this.age = age; }  } 

So, to transform it into a JSON object, it's very simple. Like this:

import org.json.JSONObject;  public class JsonTest {      public static void main( String[] args ) {         Person person = new Person();         person.setName( "Person Name" );         person.setAge( 333 );                  JSONObject jsonObj = new JSONObject( person );         System.out.println( jsonObj );     }  } 

Here there is another example, in this case using Jackson: https://brunozambiazi.wordpress.com/2015/08/15/working-with-json-in-java/

Maven:

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

And a link (below) to find the latest/greatest version:

https://search.maven.org/classic/#search%7Cga%7C1%7Cg%3A%22com.fasterxml.jackson.core%22%20AND%20a%3A%22jackson-databind%22

like image 170
bruno.zambiazi Avatar answered Oct 01 '22 02:10

bruno.zambiazi


If you are aware of Jackson 2, there is a great tutorial at mkyong.com on how to convert Java Objects to JSON and vice versa. The following code snippets have been taken from that tutorial.

Convert Java object to JSON, writeValue(...):

ObjectMapper mapper = new ObjectMapper(); Staff obj = new Staff();  //Object to JSON in file mapper.writeValue(new File("c:\\file.json"), obj);  //Object to JSON in String String jsonInString = mapper.writeValueAsString(obj); 

Convert JSON to Java object, readValue(...):

ObjectMapper mapper = new ObjectMapper(); String jsonInString = "{'name' : 'mkyong'}";  //JSON from file to Object Staff obj = mapper.readValue(new File("c:\\file.json"), Staff.class);  //JSON from URL to Object Staff obj = mapper.readValue(new URL("http://mkyong.com/api/staff.json"), Staff.class);  //JSON from String to Object Staff obj = mapper.readValue(jsonInString, Staff.class); 

Jackson 2 Dependency:

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

For the full tutorial, please go to the link given above.

like image 27
Bilesh Ganguly Avatar answered Oct 01 '22 04:10

Bilesh Ganguly