我正在写我的第一个Gson样本。现在我知道如何解析基本的JsonObject / JsonArray,如下所示。
成功的JSON示例:
{ "name":"A",
"title":[ {"name":"B"},
{"name":"C"}]
}
班级代码:
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代码:
Gson gson = new Gson();
Person result = gson.fromJson("JSONString", Person.class);
但现在我有了这个嵌套的Json,我不知道如何区分Class Person中的 private ArrayList <Name>
。 使用JAVA界面?TypeAdapter?
{ "name":"A",
"title":[ {"name":"B"},
{"name":"C",
"title":["name":"a","name":"b"]
},
{"name":"D"},
{"name":"E",
"title":["name":"a","name":"b"]
}
]
}