Mag Web Technologies is a Software Development Company offers on & offshore Software Development Services. Mag Web Enterprise IT services deliver excellence to new age IT businesses. Explore new possibilities of enterprise automation.
Tuesday, 29 January 2013
Marshalling Java Objects to JSON objects
A tutorial on how to create JSON object from Java objects
Gson API is a very useful API that enables you to marshal Java objects to JSON objets and unmarshall json objects to java objects.
In this tutorial we will see how easy we can marshall an object model to JSON objects.
As an example consider the case were we have a simple model that consists of tutorial categories and tutorials.
In the below snipet are the Tutorial and Category classes
package com.javaonly.json;
import java.util.Set;
public class Category {
private int id;
private String categoryName;
private Set subCategories;
private Category parentCategory;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Category getParentCategory() {
return parentCategory;
}
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}
public String toString(){
return getCategoryName();
}
public Set getSubCategories() {
return subCategories;
}
public void setSubCategories(Set subCategories) {
this.subCategories = subCategories;
}
}
package com.javaonly.json;
import java.util.Set;
public class Category {
private int id;
private String categoryName;
private Set subCategories;
private Category parentCategory;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Category getParentCategory() {
return parentCategory;
}
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}
public String toString(){
return getCategoryName();
}
public Set getSubCategories() {
return subCategories;
}
public void setSubCategories(Set subCategories) {
this.subCategories = subCategories;
}
}
Suppose now that we want to represent a java tutorial in json format.In other words we need to create the following JSON object:
{
"id":1,
"title":"Java Objects to JSON format",
"summary":"This is the summary",
"body":"This is the body",
"negativeVotes":0,
"possitiveVotes":0,
"category":{
"id":1,
"categoryName":"JAVA"
},
"dateAdded":"Nov 30, 2012 3:03:22 PM"
}
Using toJSon() method from Gson class:
package com.javaonly.json;
import java.util.Date;
import com.google.gson.Gson;
public class JsonTest {
public static void main(String args[]){
Category java=new Category();
java.setId(1);
java.setCategoryName("JAVA");
Tutorial tutorial=new Tutorial();
tutorial.setId(1);
tutorial.setCategory(java);
tutorial.setTitle("Java Objects to JSON format");
tutorial.setSummary("This is the summary");
tutorial.setBody("This is the body");
tutorial.setDateAdded(new Date());
Gson gson = new Gson();
String json = gson.toJson(tutorial);
System.out.println(json);
}
}
On the other hand the unmarshalling of a JSON object is quite simple too:
package com.javaonly.json;
import java.util.Date;
import com.google.gson.Gson;
public class JsonTest {
public static void main(String args[]){
Category java=new Category();
java.setId(1);
java.setCategoryName("JAVA");
Tutorial tutorial=new Tutorial();
tutorial.setId(1);
tutorial.setCategory(java);
tutorial.setTitle("Java Objects to JSON format");
tutorial.setSummary("This is the summary");
tutorial.setBody("This is the body");
tutorial.setDateAdded(new Date());
Gson gson = new Gson();
String json = gson.toJson(tutorial);
System.out.println(json);
Tutorial jsonTutorial=gson.fromJson(json, Tutorial.class);
System.out.println("TUTORIAL:"+jsonTutorial.getCategory().getCategoryName());
}
}
In the above example note that the category json object is unmarhalled in Category object and you can access it with getCategory() method
Labels:
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.