2

I am writing my first Gson sample. Now I know how to parse basic JsonObject / JsonArray like as below.

Successful JSON sample:

   { "name":"A",
      "title":[ {"name":"B"},
                {"name":"C"}]
    }

Class code:

public class Person {
    private String name;
    private ArrayList<Name> title;

    public Person(){
    }

    public String getname(){
        return name;
    }

    public void setname(String name){
        this.name = name;
    }
    public ArrayList<Name> getTitle(){
        return title;
    }

    public void setTitle(ArrayList<Name> title) {
        this.title = title;
    }
}

public class Name{
    private String name;

    public String getname(){
        return name;
    }

    public void setname(String name){
        this.name = name;
    }
}

Jave code:

Gson gson = new Gson();
Person result = gson.fromJson("JSONString", Person.class);  

But now I have this Nested Json, I don't know how to distinguish private ArrayList <Name> in Class Person. Use JAVA interface?TypeAdapter?

{ "name":"A",
  "title":[ {"name":"B"},
            {"name":"C",
             "title":["name":"a","name":"b"]
            },
            {"name":"D"},
            {"name":"E",
              "title":["name":"a","name":"b"]
            }
          ]
}
1

0

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.