class Admit {
public Person p;
public Student s;
public void printAdmit() {
System.out.println(p.getFirstName());
}
class Student {
public long rollno;
public String toString() {
return "rollno: " + rollno;
}
public void setRollno(long rollno) {
this.rollno = rollno;
}
}
class Person {
public String firstName;
public String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setEmail(String email) {
this.email = email;
}
public String toString() {
return "name: " + firstName + "email: " + email;
}
}
}
class JSONParsing{
public static void main(String [] args) throws Exception {
String json = "{'student':{'rollno':3},'person':{'firstName':'pavan','email':'[email protected]'}}";
Gson gson = new Gson();
//String stud = (String)jO.getString("student");
Admit add = gson.fromJson(json, Admit.class);
add.printAdmit();
}
}
I want to convert the given JSON to 2 different java objects (i.e. Student and Person). How can I achieve this in JSON. Is inner class the only way to do it? (Even this does not work). I've tried doing it through containership as well as inner classes too. If there are any other libraries please help.