Search code examples
javajsonweb-servicesrestrestlet

Restlet throwing error when JSON begins with [


I'm using Restlet to consume a json webservice and I'm getting this error:

A JSONObject text must begin with '{' at character 1

The json response I'm getting begins with a [, which seems to be causing the issue.

Is there a way to work around this?

Here is my code:

ClientResource resource = new ClientResource(
    "https://api.prosper.com/api/Listings?$top=3");
resource.setChallengeResponse(
    ChallengeScheme.HTTP_BASIC, "username", "password");
Representation representation = resource.get();
JsonRepresentation jsonRepresentation = new JsonRepresentation(representation);
JSONObject jsonObject = jsonRepresentation.getJsonObject();

Solution

  • Json that starts with a [ is a json array. Json that starts with { is a json object.

    Use JsonRepresentation#getJsonArray()

    JSONArray jsonArray = jsonRepresentation.getJsonArray();
    

    Before you continue, familiarize yourself with the json format.