I am having a class like following,
public class Student {
public int id;
public String name;
public int age;
}
Now I want to c
Better is to use @expose annotation like
public class Student {
public int id;
@Expose
public String name;
@Expose
public int age;
}
And use below method to get Json string from your object
private String getJsonString(Student student) {
// Before converting to GSON check value of id
Gson gson = null;
if (student.id == 0) {
gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
} else {
gson = new Gson();
}
return gson.toJson(student);
}
It will ignore id column if that is set to 0, either it will return json string with id field.