Gson is json library for java,which is created by google, used to generate json. By using Gson, we can generate json and convert json to java objects.

we can create gson object in two ways. They are


//Direct Initialization
 
Gson gson = new Gson();
 
//or 
//we can set our formatting options 
 
Gson gson = new GsonBuilder()
             .disableHtmlEscaping()
             .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
             .setPrettyPrinting()
             .serializeNulls()
             .create();

we can directly pass single variable(any object or primitives),that will be converted to json format.But this is still controversial one. Because it won't give result as key value pair.


import com.google.gson.Gson;

public class GsonExample {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Gson gson=new Gson();
    String name="raja";
    
    System.out.println(gson.toJson(name));
  }

}

output:

"raja"   //expected format is {"name":"raja"}

Create an Employee class with name and age as its member and generate setters and getters method for those member variable. And pass employee object to gson to covert java object to json format.

Employee class


public class Emp {
String name;
int age;
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
public int getAge() {
  return age;
}
public Emp(String name, int age) {
  super();
  this.name = name;
  this.age = age;
}
public Emp() {
  super();
}
public void setAge(int age) {
  this.age = age;
}
}

Emp class has two constructors. So we can create object ,by calling empty constructor or parameterized constructor.


import com.google.gson.Gson;

public class GsonExample {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Gson gson=new Gson();
    Emp emp=new Emp("raja",25);
    
    System.out.println(gson.toJson(emp));
  }

}

output:
{"name":"raja","age":25}

Array of Emp:

We can generate array of employee by adding employee object to ArrayList and pass arraylist object into gson.


import java.util.ArrayList;

import com.google.gson.Gson;

public class GsonExample {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Gson gson=new Gson();
    ArrayList<Emp> data=new ArrayList<Emp>();
    for(int i=0;i<10;i++)
    {
    Emp emp=new Emp("emp"+i,25+i);
    data.add(emp);
    }
    System.out.println(gson.toJson(data));
  }

}

output:
[{"name":"emp0","age":25},{"name":"emp1","age":26},
{"name":"emp2","age":27},{"name":"emp3","age":28},
{"name":"emp4","age":29},{"name":"emp5","age":30},
{"name":"emp6","age":31},{"name":"emp7","age":32},
{"name":"emp8","age":33},{"name":"emp9","age":34}]
Convert JSON to Java Objects

Its very easy to convert to java object from json.

Step1: create gson object.

Step2: call fromJson method in gson oject and pass the json data and matching class for the data. This will create an object of the particular class


import com.google.gson.Gson;

public class ReverseJson {

  
  public static void main(String[] args)
  {
    Gson gson = new Gson();
     
    Emp emp=gson.fromJson("{'name':'raja','age':25}",
          Emp.class);
    
    System.out.println(emp.getName());  
  }
}

output:
raja