使用gson将json转换为java对象

时间:2016-05-11 10:00:43

标签: java json gson

作为我的计算机科学IA的一部分,我正在创建一个工具,可以读取匹配历史和dota游戏的详细信息,并生成统计数据和英雄统计数据。要做到这一点,我已经访问了阀门API并抓住了几个匹配的jsons并从中匹配历史记录,然后将它们略微削减,以便它们只包含我在json中需要的信息。

以下是json格式的其中一个匹配项的详细信息示例:

"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
}

使用intelliJ插件我创建了3个Java类,一个具有匹配结果,一个用于结果的详细信息,一个用于结果中的参与者的详细信息,每个都有变量获取集:

TestMatch字段:

private TestMatchResult result;

TestMatchResult字段:

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字段:

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;

我已下载并将gson库作为依赖项添加到intelliJ项目中。 我试图将json解析为java类作为对象,并希望为所有匹配jsons做到这一点,但是我不太确定如何做到这一点,我只有:

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

能比我更了解gson的人能给我一些关于如何将json解析成类(es)作为几个匹配jsons的对象的指导吗?一旦我完成了这一切,我需要做的其余事情很简单,因为它只是一个获取变量并在其上运行计算然后显示它们的情况。如果它不可能或不实用,我可以制作一个测试CSV并从中读取,因为我知道如何使用它们,但只是遇到jsons,因为这是阀门API返回请求所以我想也可以学习如何使用它们。

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要使用方法Gson.fromJson()

实施例

public static void getMatch()
{
    Gson gson = new Gson();
    TestMatch tm = gson.fromJson(jsonString, TestMatch.class);
}