0
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.

0

2 Answers 2

2

Here in JSON data, variable names are person and student. SO you have to define same name in your POJO class also. so for this case you have to rename variables to :

public person person;
public student student;

If you don't want to change the name of variable then you can define their JSON variable name by using SerializedName annotation :

@SerializedName("person")
public person p;

@SerializedName("student")
public student s;

One more thing : According to JAVA coding standards, Class name should start with capital letter. So it would be better if you use class name as Person , Student, Admit respectively.

Sign up to request clarification or add additional context in comments.

1 Comment

And will it work without the inner class as well? for suppose I've separate Admit, Student and Person classes?
0

Change your Admit class object as below.

When you map your json objects to your varaiables in your corresponding POJO class , Both name should be same.The name of the json object should match with the binding variable name

class Admit
   {

       public person person;

       public student student;

       public void printAdmit(){
           System.out.println(person.getFirstName());
       }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.