Gson在Nested JsonArray中解析不同的Json对象类

时间:2015-01-24 17:12:32

标签: java android arrays json gson

我正在写我的第一个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"]
            }
          ]
}

0 个答案:

没有答案