import com.google.gson.Gson; public class JavaToJsonExample { public static void main(String[] args) { Person person = new Person(); person.setName("Ali"); person.setAge(30); Gson gson = new Gson(); String jsonString = gson.toJson(person); System.out.println("JSON: " + jsonString); } } class Person { private String name; private int age; // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }