简体   繁体   中英

Deserialization complex Json (class)

I have a Json File like this.

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

So I want to Deserialie this in C# . so I create with json2csharp.com the Classes.

Now its possible to instantiate all these classes, but this is bad I don't need a instance of GameStats, or average_stats.

What's my line here, how I can make this class not creatable?

sorry for my english, hope you can follow my problem :D

best regards. alex

Simply remove unwanted properties form 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; }
}

Deserializer omits them.

So you mean you don't know how to use a deserializer? Take a look at this link

Simply do it like this:

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

Make your classes nested to stop classes being shown in intellisense and add parameterless private constructors. Don't worry about private constructors, Json.NET uses reflection to instantiate your classes.

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

Compiler gives error:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM