任何人都可以帮助我将我的JSON反序列化为C#对象吗?

时间:2014-05-28 09:46:52

标签: c# json serialization json.net deserialization

我正在尝试将JSON转换为对象以轻松访问统计信息。我目前得到的词典和词典的空值。如果有人能提供帮助,我们将不胜感激。

以下是我的JSON示例:

    {
"teamStats": {
    "game2998": {
        "dateTime": "2014-05-23T19:00Z",
        "team304": {
            "teamId": 304,
            "teamName": "Cloud9 ",
            "matchVictory": 1,
            "matchDefeat": 0,
            "baronsKilled": 2,
            "dragonsKilled": 4,
            "firstBlood": 0,
            "firstTower": 0,
            "firstInhibitor": 1,
            "towersKilled": 10
        },
        "team1": {
            "teamId": 1,
            "teamName": "Team SoloMid",
            "matchVictory": 0,
            "matchDefeat": 1,
            "baronsKilled": 0,
            "dragonsKilled": 1,
            "firstBlood": 1,
            "firstTower": 1,
            "firstInhibitor": 0,
            "towersKilled": 5
        },
        "matchId": "2515"
    }
},
"playerStats": {
    "game2998": {
        "dateTime": "2014-05-23T19:00Z",
        "matchId": "2515",
        "player328": {
            "playerId": 328,
            "kills": 1,
            "deaths": 1,
            "assists": 7,
            "minionKills": 254,
            "doubleKills": 0,
            "tripleKills": 0,
            "quadraKills": 0,
            "pentaKills": 0,
            "playerName": "Balls",
            "role": "Top Lane"
        },
        "player329": {
            "playerId": 329,
            "kills": 4,
            "deaths": 0,
            "assists": 7,
            "minionKills": 155,
            "doubleKills": 1,
            "tripleKills": 0,
            "quadraKills": 0,
            "pentaKills": 0,
            "playerName": "Meteos",
            "role": "Jungler"
        }
    }
}

}

以下是我的课程示例:

 public class RootObject
{
    public Dictionary<string, GameT> teamStats { get; set; }
    public Dictionary<string, GameP> playerStats { get; set; }
}

public  class GameT
{
    public string dateTime { get; set; }
   public Dictionary<string, Team> blueTeam { get; set; }
   public Dictionary<string, Team> purpleTeam { get; set; }
    public string matchId { get; set; }
}

public class Team
{
    public int teamId { get; set; }
    public string teamName { get; set; }
    public int matchVictory { get; set; }
    public int matchDefeat { get; set; }
    public int baronsKilled { get; set; }
    public int dragonsKilled { get; set; }
    public int firstBlood { get; set; }
    public int firstTower { get; set; }
    public int firstInhibitor { get; set; }
    public int towersKilled { get; set; }
}
public class GameP
{
    public string dateTime { get; set; }
    public string matchId { get; set; }
    public Dictionary<string, Player> blueTop { get; set; }
    public Dictionary<string, Player> blueJungle { get; set; }




}
public class Player
{
    public int playerId { get; set; }
        public int kills { get; set; }
        public int deaths { get; set; }
        public int assists { get; set; }
        public int minionKills { get; set; }
        public int doubleKills { get; set; }
        public int tripleKills { get; set; }
        public int quadraKills { get; set; }
        public int pentaKills { get; set; }
        public string playerName { get; set; }
        public string role { get; set; }

}

我的空问题的屏幕截图: http://i.imgur.com/XdYwlXo.gif

2 个答案:

答案 0 :(得分:1)

由于您的JSON将团队和玩家列表与游戏属性(日期和匹配ID)混合在一起,因此您需要使用自定义JsonConverter使其适用于类中的强类型词典。我将在下面解释你如何做到这一点。

首先,像这样定义你的类:

public class RootObject
{
    public Dictionary<string, Game<Team>> teamStats { get; set; }
    public Dictionary<string, Game<Player>> playerStats { get; set; }
}

public class Game<T>
{
    public string dateTime { get; set; }
    public string matchId { get; set; }
    public Dictionary<string, T> items { get; set; }
}

public class Team
{
    public int teamId { get; set; }
    public string teamName { get; set; }
    public int matchVictory { get; set; }
    public int matchDefeat { get; set; }
    public int baronsKilled { get; set; }
    public int dragonsKilled { get; set; }
    public int firstBlood { get; set; }
    public int firstTower { get; set; }
    public int firstInhibitor { get; set; }
    public int towersKilled { get; set; }
}

public class Player
{
    public int playerId { get; set; }
    public int kills { get; set; }
    public int deaths { get; set; }
    public int assists { get; set; }
    public int minionKills { get; set; }
    public int doubleKills { get; set; }
    public int tripleKills { get; set; }
    public int quadraKills { get; set; }
    public int pentaKills { get; set; }
    public string playerName { get; set; }
    public string role { get; set; }
}

定义一个通用的JsonConverter,它可以处理JSON的“Game”部分,将日期和匹配ID与其余项目分开:

public class GameConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(Game<T>));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);
        Game<T> game = new Game<T>();
        game.dateTime = jo["dateTime"].ToString();
        game.matchId = jo["matchId"].ToString();
        game.items = new Dictionary<string, T>();
        foreach (JProperty prop in jo.Properties())
        {
            if (prop.Value.Type == JTokenType.Object)
            {
                game.items.Add(prop.Name, prop.Value.ToObject<T>());
            }
        }
        return game;
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

使用转换器后,您可以按如下方式反序列化(请注意,我们需要两个转换器实例,一个用于处理团队列表,另一个用于处理播放器列表):

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new GameConverter<Team>());
settings.Converters.Add(new GameConverter<Player>());

RootObject root = JsonConvert.DeserializeObject<RootObject>(json, settings);

答案 1 :(得分:0)

你的JSON中没有名为blueTeam和purpleTeam的属性。

也许它应该是这样的:

    "teamStats": {
        "game2998": {
            "dateTime": "2014-05-23T19:00Z",
            "blueTeam": {
                "team304": {
                    "teamId": 304,
                    [...]
            }},
            "purpleTeam": {
                "team1": {
                    "teamId": 1,
                    [...]
            }},
            "matchId": "2515"
        }