0
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

This is the error I continue to get while attempting to parse my incoming JSON response data. I'm utilizing the OkHttp library to create and call, and the API I'm getting results from returns everything in an Array as follows:

    [
      {
        "id": 4256,
        "image_url": "https://cdn.pandascore.co/images/league/image/4256/OMEN_Challenger_Series_2019.png",
        "live_supported": false,
        "modified_at": "2019-10-30T10:02:42Z",
        "name": "OMEN Challenger",
        "series": [
          {
            "begin_at": "2019-11-01T03:30:00Z",
            "description": null,
            "end_at": null,
            "full_name": "2019",
            "id": 1932,
            "league_id": 4256,
            "modified_at": "2019-10-30T09:11:40Z",
            "name": null,
            "prizepool": "50000 United States Dollar",
            "season": null,
            "slug": "cs-go-omen-challenger-2019",
            "winner_id": null,
            "winner_type": null,
            "year": 2019
          }
        ],
        "slug": "cs-go-omen-challenger",
        "url": "https://omengaming.co/omen_cs/",
        "videogame": {
          "current_version": null,
          "id": 3,
          "name": "CS:GO",
          "slug": "cs-go"
        }
      },
      {...},
      {...},
      {...},
      {...},
    ]

I found a lot of folks recommending Gson to parse it into a custom class, but the following code, in theory, should work and it isn't. The parsing doesn't even begin due to it expecting BEGIN_OBJECT and it being BEGIN_ARRAY:

String jsonData = response.body().string();
Gson gson = new Gson();
EventInfo test = gson.fromJson(jsonData, EventInfo.class);

class EventInfo {

    String imageURL;
    String name;
    JSONArray series;

}
1
  • In your response you are getting List<EventInfo> not just EventInfo. That's why you are getting expecting BEGIN_OBJECT but was BEGIN_ARRAY. Commented Nov 2, 2019 at 7:24

3 Answers 3

1

You are trying to parse it into an object. But in your response, you can clearly see that it's a list. The parent POJO should have been a list. And inside that list, you should have created another POJO.

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

1 Comment

It's interesting because I"d been using the method to parse it as a List<EventInfo> and still had the error, just in a different spot. The original issue was trying to parse series as a JSONArray. Full solution being added to the post as we speak! Thank you for encouraging me to give the List<> another go. --KH
0

In your response parent is found as array but you need to add first parent as JSON object and child as a array or object.

You need response like this

{
    "YourArrayName":[
        "YourChildObjName":{
            "id": 4256,
            "image_url": "https://cdn.pandascore.co/images/league/image/4256/OMEN_Challenger_Series_2019.png",
            "live_supported": false,
            "modified_at": "2019-10-30T10:02:42Z",
            "name": "OMEN Challenger",
            "series": [
                {
                    "begin_at": "2019-11-01T03:30:00Z",
                    "description": null,
                    "end_at": null,
                    "full_name": "2019",
                    "id": 1932,
                    "league_id": 4256,
                    "modified_at": "2019-10-30T09:11:40Z",
                    "name": null,
                    "prizepool": "50000 United States Dollar",
                    "season": null,
                    "slug": "cs-go-omen-challenger-2019",
                    "winner_id": null,
                    "winner_type": null,
                    "year": 2019
                }
            ],
            "slug": "cs-go-omen-challenger",
            "url": "https://omengaming.co/omen_cs/",
            "videogame": {
                "current_version": null,
                "id": 3,
                "name": "CS:GO",
                "slug": "cs-go"
            }
        },
        {...},
        {...},
        {...},
        {...},
    ]
}

I hope this can help You!

Thank You

2 Comments

Thank you for this! Unfortunately I don't have any control over the API, but I found the solution. I had a 2-part problem, actually. :)
@KevinHaube OK.
0

So, I figured it out. Originally I was receiving the same error at a later point; namely when it got to the series key value in the first JSONObject. The original error occurred because I was trying to parse series as a JSONArray, rather than a List<JSONObject> The corrections are below:

String jsonData = response.body().string();
Gson gson = new Gson();
Type listType = new TypeToken<List<EventInfo>>() {}.getType();
List<EventInfo> test = gson.fromJson(jsonData, listType);

And the EventInfo class:

class EventInfo {

    String imageURL;
    String name;
    List<JSONObject> series;

}

Thank you for the advice everyone!

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.