反序列化复杂Json(类)

时间:2016-09-05 08:18:55

标签: c# json json.net

我有一个像这样的Json文件。

{
"_request": {
    "api_ver": 1,
    "route": "/api/v2/u/SunDwarf-21353/stats/general"
},
"average_stats": {
    "damage_done_avg": 3987.0,
    "deaths_avg": 5.68,
    "defensive_assists_avg": 0.0,
    "eliminations_avg": 10.47,
    "final_blows_avg": 6.12,
    "healing_done_avg": 589.0,
    "melee_final_blows_avg": 0.03,
    "objective_kills_avg": 3.06,
    "objective_time_avg": 0.007222222222222223,
    "offensive_assists_avg": 0.0,
    "solo_kills_avg": 2.3,
    "time_spent_on_fire_avg": 0.008055555555555555
},
"battletag": "SunDwarf-21353",
"game_stats": {
    "cards": 36.0,
    "damage_done": 478462.0,
    "damage_done_most_in_game": 13303.0,
    "deaths": 682.0,
    "defensive_assists": 39.0,
    "defensive_assists_most_in_game": 11.0,
    "eliminations": 1257.0,
    "eliminations_most_in_game": 26.0,
    "environmental_deaths": 12.0,
    "environmental_kills": 8.0,
    "final_blows": 735.0,
    "final_blows_most_in_game": 16.0,
    "games_played": 120.0,
    "games_won": 59.0,
    "healing_done": 70670.0,
    "healing_done_most_in_game": 7832.0,
    "kpd": 1.84,
    "medals": 304.0,
    "medals_bronze": 102.0,
    "medals_gold": 100.0,
    "medals_silver": 102.0,
    "melee_final_blows": 4.0,
    "melee_final_blows_most_in_game": 2.0,
    "multikill_best": 3.0,
    "multikills": 5.0,
    "objective_kills": 368.0,
    "objective_kills_most_in_game": 10.0,
    "objective_time": 0.8880555555555555,
    "objective_time_most_in_game": 0.026944444444444444,
    "offensive_assists": 13.0,
    "offensive_assists_most_in_game": 7.0,
    "recon_assists": 9.0,
    "solo_kills": 277.0,
    "solo_kills_most_in_game": 16.0,
    "time_played": 15.0,
    "time_spent_on_fire": 0.9961111111111111,
    "time_spent_on_fire_most_in_game": 0.08833333333333333
},
"overall_stats": {
    "avatar": "https://blzgdapipro-a.akamaihd.net/game/unlocks/0x02500000000008E8.png",
    "comprank": null,
    "games": 120,
    "level": 24,
    "losses": 61,
    "prestige": 0,
    "win_rate": 49,
    "wins": 59
},
"region": "eu"
}

所以我想在C#中使用Deserialie。所以我用json2csharp.com创建了Classes。

现在可以实例化所有这些类,但这很糟糕我不需要GameStats或average_stats的实例。

我的界限是什么,如何让这个课程无法创造?

抱歉我的英语,希望你能解决我的问题:D

最好的问候。亚历

3 个答案:

答案 0 :(得分:1)

只需从RootObject

中删除不需要的属性即可
public class RootObject
{
    public Request _request { get; set; }
    //public AverageStats average_stats { get; set; }
    public string battletag { get; set; }
    //public GameStats game_stats { get; set; }
    public OverallStats overall_stats { get; set; }
    public string region { get; set; }
}

反序列化器省略了它们。

答案 1 :(得分:0)

所以你的意思是你不知道如何使用解串器? 看看this link

简单地这样做:

common

答案 2 :(得分:0)

使您的类嵌套以停止在intellisense中显示的类并添加无参数的私有构造函数。不要担心私有构造函数,Json.NET使用反射来实例化你的类。

public class RootObject
{
    public class GameStats
    {
        private GameStats() { }
        //Code omitted
    }

    public class Request
    {
        private Request() { }
        //Code omitted
    }

    public class AverageStats
    {
        private AverageStats() { }
        //Code omitted
    }

    public class OverallStats
    {
        private OverallStats() { }
        //Code omitted
    }

    public Request _request { get; set; }
    public AverageStats average_stats { get; set; }
    public string battletag { get; set; }
    public GameStats game_stats { get; set; }
    public OverallStats overall_stats { get; set; }
    public string region { get; set; }
}

编译器给出错误:

//'RootObject.GameStats.GameStats()' is inaccessible due to its protection level
var myClass = new RootObject.GameStats();