Java Language JSON To Object (Gson Library)


Example

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 gson = new Gson();
String json = "{\"name\": \"John\"}";

Person person = gson.fromJson(json, Person.class);
System.out.println(person.name); //John

You must have gson library in your classpath.