简体   繁体   中英

How to deserialize object or array

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

To get to the name you need to have the following path:

children.name

If you have an array of firstnames, you should access as an array:

children.name.firstname[number]

Since your "name" is an Object type, you can refer to any of it's fields via "."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM