Java Language JSON in Java Object To JSON (Gson Library)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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 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.



Got any Java Language Question?