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

  • class Root{ List persons; Job job; Child} – Prinz Private Feb 21 '13 at 13:33
  • The above comment is not valid Java. Please post the class you are trying to deserialize into, as well as the error (if any) or other symptoms that you are seeing, and what you expect to see. – Eric Galluzzo Feb 21 '13 at 13:38
  • possible duplicate of [Parsing JSON with GSON, object sometimes contains list sometimes contains object](http://stackoverflow.com/questions/6223023/parsing-json-with-gson-object-sometimes-contains-list-sometimes-contains-object) – Brian Roach Feb 21 '13 at 14:26
  • 1
    As I understand the problem description, custom deserialization is necessary to handle the situation where the JSON is sometimes an array and sometimes an object. Using Gson to handle this specific issue has been covered in previous StackOverflow threads, such as http://stackoverflow.com/questions/6223023/parsing-json-with-gson-object-sometimes-contains-list-sometimes-contains-object/6225906#6225906 and http://stackoverflow.com/questions/7668507/gson-handle-object-or-array. This information is based on an older version of Gson. It's possible that a newer release added or will add a configurati – Programmer Bruce Feb 21 '13 at 14:23

1 Answers1

0

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 "."

user
  • 3,058
  • 23
  • 45
  • I think the OP is trying to deserialize this JSON into Java and having some sort of trouble doing so. It's still unclear what the error is, though. – Eric Galluzzo Feb 21 '13 at 13:37
  • Well, he did ask about the document and the document is present as code. If he attaches more details, it would be more clear, I agree. – user Feb 21 '13 at 13:44
  • Sure, good point! We'll just have to see what the actual problem is. – Eric Galluzzo Feb 21 '13 at 13:49
  • i'm getting JSONReader java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 5161216 – Prinz Private Feb 21 '13 at 13:59
  • 1
    So why don't you provide the correct code, full stacktrace of exception and form your question more clear? Also, I'm a bit confused of "column 516216" in line 1(!) ... Can you imagine that document? – user Feb 21 '13 at 14:00
  • Well, just copy and past it to here via "edit" button. Look it right below the post you created. I will edit it if it is not going to look in proper way. – user Feb 21 '13 at 14:23
  • i get "Your edit couldn't be submitted. Please see the error above." more details.. – Prinz Private Feb 21 '13 at 14:38
  • Please, indicate what error do you get, It has to be shown above while editing. – user Feb 21 '13 at 14:41
  • @ConstantineNovykov - His problem is that `name` inside the `Child` object can either be an object or an array, and that's why this is a dup. He has to handle this exactly as the answer to the question this is a dup of states. And that's ignoring that his `Root` class currently has `Child[]` which is completely incorrect; that's never an array. – Brian Roach Feb 21 '13 at 17:59
  • thanks i found the answer under the link from "programmer Bruce" thanks a lot – Prinz Private Feb 25 '13 at 11:11