How To Convert Java Object to JSON using Google’s Gson

Connect with

How To Convert Java Object to JSON using Google’s Gson

There are different library available to convert from java object to JSON and JSON to Java Object. The 2 are most popular first one is Google’s Gson API and another one is Jackson API.

Note: here orderBy parameter provided more than once, it means , sorting can be on multiple parameter and when you provide same key multiple times , jersey implementation convert int into list smartly.

1. Maven dependency

You can choose version of jersey as per your choice or download Gson jar file from Google and include in your project lib.



	com.google.code.gson
	gson
	2.4

2. Custom Java Object with default Gson constructor

Call this method in java main method to check the output of this.

/**
 * This return a JSON string by converting from Java {@link Person} object.
 * with default Gson constructor.
 * 
 * @return
 */
private static String getJsonString() {
	String jsonData = null;
	try {
		Person person = new Person(1, "ranjeet", new Date());
		Gson gson = new Gson();
		jsonData  = gson.toJson(person);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonData;
}

Json Output:

{"id":1,"name":"ranjeet","doj":"Jan 6, 2016 10:13:27 AM"}

3. Custom Java Object with GsonBuilder constructor

Google’s GSON API handles the conversions between Java and JSON objects. An instance of this class can be created by invoking the default constructor or using the GsonBuilder class. The GsonBuilder class provides customisation, and allows developer to instantiate Gson as required. Call this method in java main method to check the output of this.

/**
 * This return a JSON string by converting from Java {@link Person} object.
 * with customer GsonBuilder .
 * 
 * @return
 */
private static String getJsonStringByGsonBuilder() {
	String jsonData = null;
	try {
		Gson gson = new GsonBuilder().create();
		Person person = new Person(1, "ranjeet", new Date());
		
		jsonData  = gson.toJson(person);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonData;
}

Json Output:

{"id":1,"name":"ranjeet","doj":"Jan 6, 2016 10:13:27 AM"}

4. Embedded Java Object

Call this method in java main method to check the output of this.

/**
 * This return a JSON string by converting from Java {@link Person} object.
 * with customer GsonBuilder .
 * 
 * @return
 */
private static String getJsonWithEmbededObject() {
	String jsonData = null;
	try {
		Gson gson = new GsonBuilder().create();
		Person person = new Person(1, "ranjeet", new Date());
		Map map = new HashMap();
		map.put("name", "ranjeet jha");
		map.put("empid", 345);
		map.put("person", person);
		
		
		jsonData  = gson.toJson(map);
		
		System.out.println("json: " + jsonData);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonData;
}

Output:

{"empid":345,"person":{"id":1,"name":"ranjeet","doj":"Jan 6, 2016 10:15:19 AM"},"name":"ranjeet jha"}

5. List Of Map

Call this method in java main method to check the output of this.

/**
 * This return a JSON string by converting from Java {@link Person} object.
 * with customer GsonBuilder .
 * 
 * @return
 */
private static String getJsonWithListOfMap() {
	String jsonData = null;
	try {
		Gson gson = new GsonBuilder().create();
		List> listMap = new ArrayList>();
		
		Map map = new HashMap();
		map.put("name", "ranjeet jha");
		map.put("empid", 345);

		listMap.add(map);
		
		Map map2 = new HashMap();
		map2.put("name", "Anushka");
		map2.put("empid", 444);
		listMap.add(map2);
		
		jsonData  = gson.toJson(listMap);
		
		System.out.println("json: " + jsonData);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return jsonData;
}

Output JSON: :

[{"empid":345,"name":"ranjeet jha"},{"empid":444,"name":"Anushka"}]

6. Download Source Code

Download source code of Gson java to Json Example

7. References

  1. Google Gson API

Connect with

1 thought on “How To Convert Java Object to JSON using Google’s Gson

  1. Hey there, You have done a great job. I’ll
    certainly digg it and personally recommend to my friends.
    I am confident they’ll be benefited from this site.

Leave a Reply

Your email address will not be published. Required fields are marked *