我有一个这样的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#中序列化它。所以我创建json2csharp.com的类。
现在可以实例化所有这些类,但这很糟糕,我不需要GameStats或average_stats的实例。
这一行是什么,我如何使这个类不可创建?
很抱歉我的英语不好,希望你能理解我的问题:D
致以最亲切的问候。亚历克斯
直接删除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; }
}
反序列化器省略它们。
你的意思是你不知道如何使用反序列化器?看看这个链接
简单地这样做:
string json = @"{
'Email': 'james@example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
]
}";
Account account = JsonConvert.DeserializeObject<Account>(json);
Console.WriteLine(account.Email);
// james@example.com
使你的类嵌套,以停止智能感知显示类,并添加无参数私有构造函数。不用担心私有构造函数,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();