Saturday 12, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 5 October 2013

How to Convert Object to JSON String using GSON


JSON – JavaScript Object Notation.
JSON is the simple, easy and lightweight framework for data exchange.It is good replacement for XML format String
GSON
GSON is java library for create JSON String. It was created for use inside Google, now its widely used by many applications
In this Tutorials we are going to see how to convert Java object to JSON String using GSON library.
1. Create project called JSONExample.
2. Create package called com.javatutorialscorner.gson 
3. Create java class called GsonPojo under com.javatutorialscorner.gson.
GsonPojo.java
GsonPojo is  Pojo class.
package com.javatutorialscorner.gson;

import java.util.List;

public class GsonPojo {

private int var1 = 10;
private String var2;
private List<String> list;

public int getVar1() {
return var1;
}

public void setVar1(int var1) {
this.var1 = var1;
}

public String getVar2() {
return var2;
}

public void setVar2(String var2) {
this.var2 = var2;
}

public List<String> getList() {
return list;
}

public void setList(List<String> list) {
this.list = list;
}

}

4. Now Create class called GSONExample under com.javatutorialscorner.gson.

GSONExample.java

This is main class.

package com.javatutorialscorner.gson;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class GSONExample {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

List<String> list = new ArrayList<String>();
list.add("Java");
list.add("Sevlet");
list.add("JSP");
list.add("JSF");
list.add("Struts 1.x");
list.add("Struts 2.x");
list.add("Tiles");
list.add("Spring");
list.add("Hibernate");

GsonPojo pojo = new GsonPojo();
pojo.setVar1(10);
pojo.setVar2("Java Tutorials Corner");
pojo.setList(list);
Gson gson = new Gson();
String json = gson.toJson(pojo);

System.out.println(json);

}

}

Now run the program see the following output in console.

{"var1":10,"var2":"Java Tutorials Corner","list":["Java","Sevlet","JSP","JSF","Struts 1.x","Struts 2.x","Tiles","Spring","Hibernate"]}

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to replace the element from List Following example shows how to replace all the occurrence of element with another element using Collections.replaceAll(list, oldVal, newVal) method in Java. <String> boolean java.util.Collections.replaceAll(List<St… Read More
  • String Concatenation in JavaFollowing example shows String concatenation in Java using + operator and StringBuffer.append() method. Sample Programpackage com.javatutorialcorner.javastring;public class StringConcatenation { public static void main(Str… Read More
  • How to shuffle the element of collection (List) Following example shows how to shuffle the element of Collection (List) using Collections.shuffle(list); method in Java. void java.util.Collections.shuffle(List<?> list)shufflepublic static void shuffle(List<?> … Read More
  • Java StAX API OverviewThe StAX API exposes methods for iterative, event-based processing of XML documents. The StAX API is bidirectional, enabling both reading and writing of XML documents.The StAX API is really two distinct API sets 1. Cursor AP… Read More
  • Java StAX Parser – Read XML using StAX Iterator API In this tutorial we are going to see how to read XML file using StAX Iterator API.For this tutorial we have following XML file that contains list of Cricket teams. crcicket.xml India MS Dhoni South Afric… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to Convert Object to JSON String using GSON Rating: 5 Reviewed By: eHowToNow