0

Using Gson (Android) to deserialize an external web-api I stumbled upon a problem.

Attribute A is a list of objects, as such:

{
    A: [
       { /* object */ },
       { /* object */ },
    ]
}

However when A only contains one object the API returns it as a single object, like this:

{
    A: { /* object */ }
}

In my target class I have A defined like this:

public class Foo {
    private AObject[] A;
}

This works fine for when attribute is a proper array but fails with following exception for the single-object version:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT

Is the any simple way to deserialize the attribute to a array containing only one item? Or do I have to loop through the response and change it before deserialization?

Thanks

Community
  • 1
  • 1
ericwenn
  • 106
  • 1
  • 10

0 Answers0