Parsing json file with gson
android, gson, json
Solution
Try calling Gson like this:
List<Venues> venues = gson.fromJson(reader, new TypeToken<List<Venues>>() {}.getType());
This works because your JSON document is a `List`, not an object that has a list as one of its properties.
Problem
I have a problem creating a mapping for a json that i want to parse using json. It's very specific, its about a json file with a json array with objects in it. my jsonfile starts like this: ``` [ { "venue": { "venue_seasons": [ { "created_at": "2011-12-25T23:00:28Z", "updated_at": "2011-12-28T15:13:53Z", "start_timestamp": 1293840000, "id": 337, "end": "2011-12-24T00:00:00Z", "enabled": true, "start": "2011-01-01T00:00:00Z", "season_openings": [ … ], "end_timestamp": 1324684800 }, { … } ], "address": "someadress", "city": "cityname", "name": "name", "created_at": "2011-03-31T07:55:33Z", etcetera } "venue":{another venue ``` So first an array, than an object (venue) with a lot of objects in it (i removed most of them, because thats not important for my question), and some arrays (like season_openings). My parsing code works like this, im using gson. The inputstream works fine. ``` Reader reader = new InputStreamReader(inputStream); JsonResponse venueResponse = gson.fromJson(reader, JsonResponse.class); List<Venues> results = venueResponse.venue; ``` with the class JsonResponse: ``` public class JsonResponse { public List<Venues> venue; } ``` and Venues.class: ``` public class Venues { public List<VenueSeasons> venue_seasons; @SerializedName("adress") public String getAdress; @SerializedName("city") public String getCity; @SerializedName("country") public String getCountry; etcetera } ``` But when i run this code i get an error: ``` Unable to start activity ComponentInfo{com.hera.android.JSON/com.hera.android.JSON.TestParser2Activity}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 ``` Ofcourse i can read the error: it expects an objects but get an array. I varied a lot with different jsonresponse.class and even with putting the whole json array in a json object (what is not really a solution because i need to work with this type of jsonfile). But everytime i get this or a similar error. I think I'm close to a solution, can anyone see what i just can't and give me a helping hand? Thanks.