0

How to handle name from children. I'm using gson. here is my code and i get illegalstateexception : expected Begin object but was begin array at line .. i don't know how to iterate gson object

        Reader reader = jSONParser.getGSONFromUrl(url);
    Root response=null;
    try {
        response = new Gson().fromJson(reader, Root.class);
        person
        = new ArrayList<Person>(Arrays.asList(response.person.clone()));
       children 
       = new ArrayList<Child>(Arrays.asList(response.Children.clone()));
       job
        = response.job;
        } catch (Exception e) {
        // TODO: handle exception
        System.out.println("JSONReader "+e.getMessage());
    }
}

public class Root{  
    @SerializedName("person")       
    Person[] persons;
    @SerializedName("job")
    Job job;
    @SerializedName("children")
    Child[] children;

}

    class Child{ 
    int cID;
    List<String>names;
}


{
person:[{}, {}, {}..], 

job:{..},

children:{"cID":"1", "name":{"firstname":"mark"}} 

}

or

{

person:[{}, {}, {}..],

job:{..},

children:{"cID":"1", "name":[{"firstname":"mark"}, {"firstname":"jan"}, {"firstname":"tamara"}...]}

}

How to handle name from children. I'm using gson. here is my code and i get illegalstateexception : expected Begin object but was begin array at line .. i don't know how to iterate gson object

4

1 回答 1

0

要获取名称,您需要具有以下路径:

children.name

如果你有一个名字数组,你应该作为一个数组访问:

children.name.firstname[number]

由于您的“名称”是一个对象类型,您可以通过“。”引用它的任何字段。

于 2013-02-21T13:27:40.950 回答