Lets assume you have a class called Person
with just name
private class Person {
public String name;
public Person(String name) {
this.name = name;
}
}
Code:
Gson g = new Gson();
Person person = new Person("John");
System.out.println(g.toJson(person)); // {"name":"John"}
Of course the Gson jar must be on the classpath.