0

I am trying to parse this JSON:

{
  "Aatrox": {
    "version": "5.2.1",
    "id": "Aatrox",
    "key": "266",
    "name": "Aatrox",
    "title": "the Darkin Blade"
  },
  "Ahri": {
    "version": "5.2.1",
    "id": "Ahri",
    "key": "103",
    "name": "Ahri",
    "title": "the Nine-Tailed Fox"
  },
  "Akali": {
    "version": "5.2.1",
    "id": "Akali",
    "key": "84",
    "name": "Akali",
    "title": "the Fist of Shadow"
  },
  ....
}

As you can see the elements all have the same attributes, so I want to parse them as a list of elements, here's the class I'm using:

public class CampeonBO {

private String version;
private String id;
private String key;
private String name;
private String title;

//Getters and Setters
}

And this is how I'm trying to parse it with Gson

Type type = new TypeToken<List<CampeonBO>>(){}.getType();
List<CampeonBO> campeones = gson.fromJson(bufferedReader, type);

And I'm getting the error:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Which is obviously because I don´t have an array in my JSON, I have 3 attributes "Aatrox, Ahri, Akali", but they all have the same attributes, so how can I parse them as a list using Gson?

dimo414
  • 47,227
  • 18
  • 148
  • 244
Luis Miguel
  • 495
  • 8
  • 22

2 Answers2

4

You're telling Gson to parse data into a List, meaning Gson expects to parse a JSON array:

JSON Array format

However the JSON you're trying to parse is in fact a JSON object:

JSON Object format

In general, the Gson way to parse a JSON object is to parse it into a dedicated Java type, as you're doing with CampeonBO. However a JSON object is also conceptually a mapping, and therefore you can use a Map as your parse Type if you're trying to parse a JSON object with arbitrary keys and the same type for all values.

Once you have a Map, you can call Map.values() to get a Collection of the map's values (which you can then put into a List if you need).

See How can I convert JSON to a HashMap using Gson? for some examples.

Images from http://json.org/.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
0

This is a JSON,

{
"Aatrox": {
    "version": "5.2.1",
    "id": "Aatrox",
    "key": "266",
    "name": "Aatrox",
    "title": "the Darkin Blade"
},
"Akali": {
    "version": "5.2.1",
    "id": "Akali",
    "key": "84",
    "name": "Akali",
    "title": "the Fist of Shadow"
}
}

an array would been :

   [
    {
        "version": "5.2.1",
        "id": "Aatrox",
        "key": "266",
        "name": "Aatrox",
        "title": "the Darkin Blade"
     },
    {
        "version": "5.2.1",
        "id": "Akali",
        "key": "84",
        "name": "Akali",
        "title": "the Fist of Shadow"
    }
  ]

please notice the Square brackets. So if possible,please modify your service

Droidekas
  • 3,464
  • 2
  • 26
  • 40
  • Thanks for the answer. I can´t modify the json so i can´t change it to square brackets. – Luis Miguel Mar 01 '15 at 17:04
  • its a string isnt it?you could modify the characters when they come in.If not.Then you need to modify your pojo to accept this input.Or manually parse the JSON – Droidekas Mar 01 '15 at 17:14
  • @LuisMiguel yes you are correct that json is incorrect otherwise `Type type = new TypeToken>() { }.getType(); List campeones = new Gson().fromJson(s, type);` will work. – TechnoCrat Mar 01 '15 at 17:17