Converting object properties/values to string or json

Hi expert,

I want to convert class properties and values to string or json. For example:
class A{
  int id;
  String name;
  public A(){
    this.id = 10;
    this.name = "somebody";
  }
}

Open in new window

The expected json string output is like:
{"id":10,"name":"somebody"}
or
id=10,name="somebody"
LVL 8
Ahmed MerghaniSoftware EngineerAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
String json = new Gson().toJson(new A());

Open in new window

http://google-gson.googlecode.com
0
 
CEHJCommented:
:)
0
All Courses

From novice to tech pro — start learning today.