0

As part of my computer science IA I am creating a tool that reads match history and details of dota games and generates stats and hero stats. To do this I have accessed the valve API and grabbed a few jsons of matches and match history from it, then cut them down slightly so they only contain the information I need in the json.

Below is a sample of the details of one of the matches in a json format:

"result": {
    "players": [
        {
            "account_id": 40884464,
            "player_slot": 0,
            "hero_id": 31,
            "kills": 8,
            "deaths": 8,
            "assists": 14,
            "last_hits": 72,
            "denies": 0,
            "gold_per_min": 304,
            "xp_per_min": 412,
            "level": 18,        
        },
        {
            "account_id": 70638797,
            "player_slot": 1,
            "hero_id": 35,
            "kills": 6,
            "deaths": 7,
            "assists": 4,
            "last_hits": 212,
            "denies": 37,
            "gold_per_min": 371,
            "xp_per_min": 356,
            "level": 17,
        },
        {
            "account_id": 76281087,
            "player_slot": 2,
            "hero_id": 5,
            "kills": 3,
            "deaths": 13,
            "assists": 10,
            "last_hits": 22,
            "denies": 0,
            "gold_per_min": 215,
            "xp_per_min": 259,
            "level": 14,
        },
        {
            "account_id": 4294967295,
            "player_slot": 3,
            "hero_id": 28,
            "kills": 11,
            "deaths": 11,
            "assists": 11,
            "last_hits": 166,
            "denies": 18,
            "gold_per_min": 413,
            "xp_per_min": 485,
            "level": 20,
        },
        {
            "account_id": 81692493,
            "player_slot": 4,
            "hero_id": 2,
            "kills": 1,
            "deaths": 9,
            "assists": 7,
            "last_hits": 135,
            "denies": 8,
            "gold_per_min": 261,
            "xp_per_min": 314,
            "level": 16,
        },
        {
            "account_id": 10101141,
            "player_slot": 128,
            "hero_id": 30,
            "kills": 7,
            "deaths": 8,
            "assists": 25,
            "last_hits": 90,
            "denies": 2,
            "gold_per_min": 382,
            "xp_per_min": 421,
            "level": 18,

        },
        {
            "account_id": 62101519,
            "player_slot": 129,
            "hero_id": 7,
            "kills": 6,
            "deaths": 8,
            "assists": 20,
            "last_hits": 305,
            "denies": 0,
            "gold_per_min": 556,
            "xp_per_min": 585,
            "level": 22,
        },
        {
            "account_id": 134700328,
            "player_slot": 130,
            "hero_id": 4,
            "kills": 17,
            "deaths": 2,
            "assists": 13,
            "last_hits": 335,
            "denies": 16,
            "gold_per_min": 729,
            "xp_per_min": 724,
            "level": 25,
        },
        {
            "account_id": 35357393,
            "player_slot": 131,
            "hero_id": 83,
            "kills": 4,
            "deaths": 4,
            "assists": 23,
            "last_hits": 16,
            "denies": 4,
            "gold_per_min": 318,
            "xp_per_min": 407,
            "level": 18,
        },
        {
            "account_id": 4294967295,
            "player_slot": 132,
            "hero_id": 101,
            "kills": 13,
            "deaths": 8,
            "assists": 12,
            "last_hits": 57,
            "denies": 3,
            "gold_per_min": 390,
            "xp_per_min": 405,
            "level": 18,
        }
    ]
    ,
    "radiant_win": false,
    "duration": 2682,
    "start_time": 1461781997,
    "match_id": 2324299045,
    "match_seq_num": 2036251155,
    "cluster": 133,
    "game_mode": 1,
    "flags": 0,
    "engine": 1,
    "radiant_score": 30,
    "dire_score": 48
}

Using an intelliJ plugin I have created 3 Java classes, one with the match result, one for the details of the result, and one for the details of the players within the result, each with the variables gets sets in:

TestMatch fields:

private TestMatchResult result;

TestMatchResult fields:

private int duration;
private int start_time;
private int cluster;
private boolean radiant_win;
private int match_seq_num;
private int engine;
private TestMatchResultPlayers[] players;
private long match_id;
private int dire_score;
private int flags;
private int game_mode;
private int radiant_score;

TestMatchResultPlayers fields:

private int kills;
private int gold_per_min;
private int last_hits;
private int account_id;
private int assists;
private int level;
private int player_slot;
private int xp_per_min;
private int hero_id;
private int denies;
private int deaths;

I have downloaded and added the gson library as a dependency into the intelliJ project. I am trying to parse the json into the java classes as an object and would like to do that for all the match jsons, however I am not quite sure how to do that at the moment, all I have is:

public static void getMatch()
{
    Gson gson = new Gson();
}

Could someone who understands gson better than myself give me a little bit of guidance as to how I'd go about parsing that json into the class(es) as an object for several match jsons? Once I've done that the rest of what I need to do is easy since it's just a case of taking the variables and running calculations on them then displaying them. If it's not possible or practical I can make a test CSV and read from that instead as I know how to use them, but only just come across jsons as that is what the valve API returns requests in so figured I may as well learn how to use them.

Thanks!

Lucky
  • 16,787
  • 19
  • 117
  • 151
Taylor
  • 31
  • 2
  • 1
    see here http://stackoverflow.com/questions/22685948/android-json-parsing-arrays-with-gson Or here http://stackoverflow.com/a/22754230/5202007 for more help – Mohammad Tauqir May 11 '16 at 10:05
  • 1
    https://github.com/google/gson/blob/master/UserGuide.md best source of info. You can also use http://www.jsonschema2pojo.org/ (or idea as you already done) to create you pojo classes and parce json string with `TestMatch myobj = new Gson().fromJson(jsonString, TestMatch.class);` – varren May 11 '16 at 10:10

1 Answers1

0

you need to use the method Gson.fromJson()

Example:

public static void getMatch()
{
    Gson gson = new Gson();
    TestMatch tm = gson.fromJson(jsonString, TestMatch.class);
}
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97