Looking for java Answers? Try Ask4KnowledgeBase
Looking for java Keywords? Try Ask4Keywords

Java Language Object To JSON (Gson Library)


Lets assume you have a class called Person with just name

private class Person {
    public String name;

    public Person(String name) {
        this.name = name;


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.