-2

I want to create a list of objects from a JSON object that internally has a lot of different objects. All of the inner objects have the same properties. They look like this:

{
    "data": {
        "MonkeyKing": {
            "title": "the Monkey King",
            "id": 62,
            "key": "MonkeyKing",
            "name": "Wukong"
        },
        "Jax": {
            "title": "Grandmaster at Arms",
            "id": 24,
            "key": "Jax",
            "name": "Jax"
        },
}

The problem is, that I do not want to access the objects using the names, but like I would with a list. A Java representation of an object would look like this:

public class Champion 
{
    String title;
    int id;
    String key;
    String name;
}

So what i want to do is, i want to put those Champion-Objects in a list using Json. My Problem is that i need to use the name of the Object to get them. So is there a way to use Gson to convert the content of the "data"- object to a list?

Peasman
  • 1
  • 2

1 Answers1

1

You could access the JSON using the keys. Have a look at this for Java code.

JSONObject parse = JSON.parseObject("");
for (Map.Entry<String, Object> entry : parse.entrySet()) {
    Champion myChamp = entry.getValue()); // possibly casting necessary
}
Community
  • 1
  • 1
skymon
  • 850
  • 1
  • 12
  • 19