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?

2
  • You have a map of Strings to CampeonBOs, not a list. Commented Mar 1, 2015 at 16:49
  • That's right @dimo414 , that's the solution, using a Map<String,CampeonBO>. Thank you so much. Commented Mar 1, 2015 at 17:10

2 Answers 2

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/.

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

1 Comment

Thank you so much! i resolved my problem using a Map.
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

3 Comments

Thanks for the answer. I can´t modify the json so i can´t change it to square brackets.
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
@LuisMiguel yes you are correct that json is incorrect otherwise Type type = new TypeToken<List<CampeonBO>>() { }.getType(); List<CampeonBO> campeones = new Gson().fromJson(s, type); will work.

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.