Here's a simple example of a Java class and how you can use Gson to work with it:
import com.google.gson.Gson;
public class MyObject {
private String name;
private int age;
public MyObject(String name, int age) {
this.name = name;
this.age = age;
}
// Getter and setter methods (or public fields) here
public static void main(String[] args) {
// Create a Gson instance
Gson gson = new Gson();
// Convert a Java object to JSON
MyObject obj = new MyObject("John", 30);
String json = gson.toJson(obj);
System.out.println("Java to JSON: " + json);
// Convert JSON to a Java object
MyObject newObj = gson.fromJson(json, MyObject.class);
System.out.println("JSON to Java: " + newObj.getName() + ", " + newObj.getAge());
}
}