Obmama
Obmama

Reputation: 81

How to only deserialize part of a Json file into a class c#

i have a Json file that contains some information i need elsewhere in my code but a lot of the information is irrelivant.

At the moment ive just put it into a dynamic object so i could check that it was all working:

var data = JsonConvert.DeserializeObject<dynamic>(response.Content);

How do i get the information i need out of the Json file and store them somewhere as variables.

All other tutorials where its stored in a class use the whole Json file and doesnt look like it would be useful in my case.

Here's the Json, i only really need the Stats section at the end of the file for what im doing

{
  "data": {
    "id": "",
    "type": "player",
    "children": [
      {
        "id": "legend_8",
        "type": "legend",
        "metadata": {
          "legend_name": "Pathfinder",
          "icon": "https://trackercdn.com/cdn/apex.tracker.gg/legends/pathfinder-tile.png",
          "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/pathfinder-concept-bg-small.jpg",
          "is_active": true
        },
        "stats": [
          {
            "metadata": {
              "key": "Kills",
              "name": "Kills",
              "categoryKey": "combat",
              "categoryName": "Combat",
              "isReversed": false
            },
            "value": 377.0,
            "percentile": 21.0,
            "displayValue": "377",
            "displayRank": ""
          },
          {
            "metadata": {
              "key": "Finishers",
              "name": "Finishers",
              "categoryKey": "game",
              "categoryName": "Game",
              "isReversed": false
            },
            "value": 39.0,
            "percentile": 0.2,
            "rank": 886,
            "displayValue": "39",
            "displayRank": "886"
          }
        ]
      },
      {
        "id": "legend_5",
        "type": "legend",
        "metadata": {
          "legend_name": "Bloodhound",
          "icon": "https://trackercdn.com/cdn/apex.tracker.gg/legends/bloodhound-tile.png",
          "bgimage": "https://trackercdn.com/cdn/apex.tracker.gg/legends/bloodhound-concept-bg-small.jpg",
          "is_active": false
        },
        "stats": [
          {
            "metadata": {
              "key": "Kills",
              "name": "Kills",
              "categoryKey": "combat",
              "categoryName": "Combat",
              "isReversed": false
            },
            "value": 235.0,
            "percentile": 16.0,
            "displayValue": "235",
            "displayRank": ""
          }
        ]
      }
    ],
    "metadata": {
      "statsCategoryOrder": [
        "combat",
        "game",
        "weapons"
      ],
      "platformId": 2,
      "platformUserHandle": "",
      "accountId": "",
      "cacheExpireDate": "11/10/2019 10:48:14 PM",
      "level": 49,
      "avatarUrl": "https://avatar-cdn.tracker.gg/api/avatar/2/",
      "countryCode": null,
      "collections": 36,
      "activeLegend": 8
    },
    "stats": [
      {
        "metadata": {
          "key": "Level",
          "name": "Level",
          "categoryKey": "combat",
          "categoryName": "Combat",
          "isReversed": false
        },
        "value": 49.0,
        "percentile": 46.0,
        "displayValue": "49",
        "displayRank": ""
      },
      {
        "metadata": {
          "key": "Kills",
          "name": "Kills",
          "categoryKey": "combat",
          "categoryName": "Combat",
          "isReversed": false
        },
        "value": 612.0,
        "percentile": 20.0,
        "displayValue": "612",
        "displayRank": ""
      },
      {
        "metadata": {
          "key": "Finishers",
          "name": "Finishers",
          "categoryKey": "game",
          "categoryName": "Game",
          "isReversed": false
        },
        "value": 39.0,
        "percentile": 0.5,
        "displayValue": "39",
        "displayRank": ""
      },
      {
        "metadata": {
          "key": "RankScore",
          "name": "Rank Score",
          "categoryKey": "game",
          "categoryName": "Game",
          "isReversed": false
        },
        "value": 64.0,
        "percentile": 21.0,
        "displayValue": "64",
        "displayRank": ""
      }
    ]
  }
}

Upvotes: 1

Views: 190

Answers (1)

Anu Viswan
Anu Viswan

Reputation: 18153

You could create a data structure which has only the relevant properties. For example,

public class StatMetaData
{
    public string key { get; set; }
    public string name { get; set; }
    public string categoryKey { get; set; }
    public string categoryName { get; set; }
    public bool isReversed { get; set; }
}

public class Stat
{
    public StatMetaData metadata { get; set; }
    public double value { get; set; }
    public double percentile { get; set; }
    public string displayValue { get; set; }
    public string displayRank { get; set; }
}

public class Data
{
    public List<Stat> stats { get; set; }
}

public class RootObject
{
    public Data data { get; set; }

}

Now you could deserialize the json as the following to retrieve the stats sections

var result = JsonConvert.DeserializeObject<RootObject>(json).data.stats;

Upvotes: 2

Related Questions