I am having a class like following,
public class Student {
public int id;
public String name;
public int age;
}
Now I want to create new Student,
//while create new student
Student stu = new Student();
stu.age = 25;
stu.name = "Guna";
System.out.println(new Gson().toJson(stu));
This gives me the following output,
{"id":0,"name":"Guna","age":25} //Here I want string without id, So this is wrong
So here I want String like
{"name":"Guna","age":25}
If I want to edit old Student
//While edit old student
Student stu2 = new Student();
stu2.id = 1002;
stu2.age = 25;
stu2.name = "Guna";
System.out.println(new Gson().toJson(stu2));
Now the output is
{"id":1002,"name":"Guna","age":25} //Here I want the String with Id, So this is correct
How can I make a JSON String with a field [At some point], without a field [at some point].
Any help will be highly appreciable.
Thanks.