0

I'm trying to create objects with this JSON:

{
"data": {
  "Prueba1": {
     "id": 266,
     "title": "Prueba1",
     "name": "Prueba1",
     "key": "Prueba1",
     "lore": "Prueba1"
  },
  "Prueba2": {
     "id": 111,
     "title": "Prueba2",
     "name": "Prueba2",
     "key": "Prueba2",
     "lore": "Prueba2"
  }
 },
"type": "prueba",
"version": "1.0"
}

The problem is that Gson is giving me the object 'data' but I can't not conver it to an array of objects.

My actual code is:

    JsonParser parser = new JsonParser();
    FileReader fr = new FileReader("route to archive json");
    JsonElement datos = parser.parse(fr);
    JsonElement heroes = datos.getAsJsonObject().get("data");

   final Gson gson = new Gson();
    final Type tipoListaEmpleados = new TypeToken<List<hero>>(){}.getType();
    final List<hero> hero = gson.fromJson(heroes, tipoListaEmpleados);

    System.out.println(hero.get(2));

But it's throwing error:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $

Any tip about the problem would be appreciated.

P.S: I understand that is giving all object, but I don't get the point of why is this happening, please help!!

EDIT: I'm working with an external API, that gives me that result, so I can't change the rest response, I need to convert to objects without adding []

EDIT2: Hero class looks like a single POJO :

public class hero
{
public int id;
public String title;
public String name;
public String key;
public String lore;
//constructor getters and setters
}
8
  • 1
    Why don't you have [ and ] if it's mention to be array of objects? It looks like a map to me. Commented Oct 17, 2016 at 13:56
  • It's an object with objects, it can't be possible?? Commented Oct 17, 2016 at 13:58
  • The outer element in your sample JSON is an object with {}. But you are trying to deserialize a List<hero> which would expect the outer element to be an array with []. This is why you're getting that error message. Commented Oct 17, 2016 at 13:59
  • The issue may be that you're trying to parse a single JSON object into a List<hero>. You haven't shown us what Hero looks like but there's no array to convert into a List in your JSON. Commented Oct 17, 2016 at 13:59
  • 1
    Having Prueba1 and Prueba2 let me thing those two should be in an array without the Prueba name for each element. Hving something like {"data":[{"id":266,...},{"id":111}]} Commented Oct 17, 2016 at 14:08

1 Answer 1

1

Your JSON doesn't actually have a JSON Array in it. Try this...although this might not be the exact structure you're looking for.

{
    "data": [{
        "Prueba1": {
            "id": 266,
            "title": "Prueba1",
            "name": "Prueba1",
            "key": "Prueba1",
            "lore": "Prueba1"
        },
        "Prueba2": {
            "id": 111,
            "title": "Prueba2",
            "name": "Prueba2",
            "key": "Prueba2",
            "lore": "Prueba2"
        }
    }],
    "type": "prueba",
    "version": "1.0"
}

You can use http://www.jsoneditoronline.org/ to verify your JSON and tweak accordingly

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

7 Comments

I tried with this JSON, but still not working with the code I posted in the question =D
@JotaGe you'll probably have to use this line too instead JsonElement heroes = datos.getAsJsonArray().get("data");
Yes, I have that line
No, you're getting it as an object and using JsonElement heroes = datos.getAsJsonObject().get("data");
Also, where is elemento in final List<hero> hero = gson.fromJson(elemento, tipoListaEmpleados); coming from?
|

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.