0

http://pastebin.com/rXbeKqAa

Hi all I have been trying to parse the above JSON into a java program and store it into an object... (Don't have a specific structure at the moment, as long as I can get the data from the object.)

Have been trying to use GSON but I can't seem to get it right..

        String inputLine="";
    HttpClient httpclient= new DefaultHttpClient();
    HttpGet method = new HttpGet("http://localhost:3000/specs/215/errors.js");
    HttpResponse response =httpclient.execute(method);
    BufferedReader in = new BufferedReader(
            new InputStreamReader(
                    response.getEntity().getContent()));

    inputLine = in.readLine();
    System.out.println(inputLine);
    in.close();
    Gson gson = new Gson();
    JsonParser parser = new JsonParser();
    JsonArray array = parser.parse(inputLine).getAsJsonArray();

    for(int i=0; i < array.size(); i++) {

        Errors e = gson.fromJson(array.get(0), Errors.class);
        System.out.println(e.error.getReason());
    }

and the error i get is:

Exception in thread "main" java.lang.IllegalStateException: This is not a JSON Array.
    at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:99)
    at test.Getter.main(Getter.java:37)

Anyone please point me in the right direction? Thanks.

1
  • (Assuming the JSON is actually all on one line.) It's not an array, it's an object containing an "errors" array, and a "warnings" array. Commented Nov 7, 2011 at 7:12

3 Answers 3

3

The JSON String is not a JSON Array String. JSON String starts and ends with {, } respectively while JSON Array starts and ends with [, ] respectively.

This line is wrong:

JsonArray array = parser.parse(inputLine).getAsJsonArray();

Rather retrieve it as a JsonObject.

Sign up to request clarification or add additional context in comments.

1 Comment

Great - thanks! By first getting it as a jsonobject and then paring it as array works. JsonObject jso = parser.parse(inputLine).getAsJsonObject(); JsonArray array = parser.parse(jso.get("warnings").toString()).getAsJsonArray();
0

JSON Structures are realized as an object, record,struct, dictionary, hash table, keyed list, or associative array.

You can use eval() function to achieve the desired goal. I think this will help you/

Comments

0

I fixed the issue I had. Novice mistake with the serialized annotations. For some reason I thought they would reference the name of the class not the field names. But more importantly, I also had to create a root-level container class to hold my classes. That allowed for all the objects to be parsed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.