Java Language JSON in Java 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) { = 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.