Problems parsing an JSON array of objects

292 views
Skip to first unread message

Danny D'Amours

unread,
May 20, 2011, 12:27:30 AM5/20/11
to google-api-java-client
I have a HttpResponse containing JSON in the format:
[{"prop1":"val1","prop2":"val2"},{"prop1":"val1","prop2":"val2"}]

Try as I might I can't get it to parse properly. I always end up with
the following stacktrace:
Exception in thread "main" java.lang.IllegalArgumentException:
START_ARRAY
at
com.google.common.base.Preconditions.checkArgument(Preconditions.java:
88)
at
com.google.api.client.json.JsonParser.startParsingObject(JsonParser.java:
161)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:233)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:224)
at
com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:
180)
at
com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:
120)
at
com.google.api.client.http.json.JsonHttpParser.parse(JsonHttpParser.java:
62)
at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:
298)


My model definition is:
public class Feed {
@Key public Item[] items;
}

and I'm calling it with:
Feed resp = response.parseAs(Feed.class);


Tracing through the code, it doesn't appear to do a check for an array
and assumes that the response is an object.
Am I missing something?

BTW, this is using 1.4.1-beta of the google-api-java-client

Yaniv Inbar (יניב ענבר)

unread,
May 21, 2011, 10:17:08 AM5/21/11
to google-api-java-client
Would you kindly elaborate on which API you are working with that returns an HTTP response formatted as an array?

You are right that this use case is currently not supported.  We hadn't anticipated that as a use case that occurs in practice.  Would you kindly file a feature request?


Until such a feature is implemented, a possible work-around for you might be to implement something like a new HttpJsonArrayParser that would call JsonParser.parseArrayAndClose().

Yaniv Inbar
Senior Software Engineer
Google Inc.

Danny D'Amours

unread,
May 21, 2011, 1:37:22 PM5/21/11
to google-api-java-client
Sure thing.

I am trying to access the Twitter user search API :
http://dev.twitter.com/doc/get/users/search which returns the JSON
results as an array of results. It appears that most of Twitter's JSON
results are returned in this format.

I've created feature 218 : http://code.google.com/p/google-api-java-client/issues/detail?id=218

Thanks for your help Yaniv,
Danny


On May 21, 11:17 am, Yaniv Inbar (יניב ענבר) <yan...@google.com>
wrote:
> Would you kindly elaborate on which API you are working with that returns an
> HTTP response formatted as an array?
>
> You are right that this use case is currently not supported.  We hadn't
> anticipated that as a use case that occurs in practice.  Would you kindly
> file a feature request?
>
> http://code.google.com/p/google-api-java-client/issues/entry?template...
Reply all
Reply to author
Forward
0 new messages