Home > Blockchain >  Iterating through keys in JSON C#
Iterating through keys in JSON C#

Time:10-09

I have a JSON file which looks like this:

[
  {
    "appid": 620,
    "playtime_forever": 841,
    "playtime_windows_forever": 841,
    "playtime_mac_forever": 0,
    "playtime_linux_forever": 0,
    "rtime_last_played": 1639764755
  },
  {
    "appid": 105600,
    "playtime_2weeks": 21,
    "playtime_forever": 25522,
    "playtime_windows_forever": 25521,
    "playtime_mac_forever": 0,
    "playtime_linux_forever": 0,
    "rtime_last_played": 1664472469
  },
  {
    "appid": 211050,
    "playtime_forever": 0,
    "playtime_windows_forever": 0,
    "playtime_mac_forever": 0,
    "playtime_linux_forever": 0,
    "rtime_last_played": 0
  },

It's only a small portion of it. It's a web response from Steam API about owned games from a user. Each game contains a key "playtime_forever". It stores minutes of playtime in a game which I want to get FOR EACH GAME, sum and write it to the console. Until now I got this:

        public const string APIKey = ProtectedAPIKey.APIKey;

        public const string SteamGamesAPILink = $"https://api.steampowered.com/IPlayerService/GetOwnedGames/v1?key={APIKey}&steamid=76561198871868188";
        public const string SteamLevelAPILink = $"https://api.steampowered.com/IPlayerService/GetSteamLevel/v1?key={APIKey}&steamid=76561198871868188";

        static WebClient wc = new();

        public static void Main()
        {

            byte[] SteamGamesAPIResponse = wc.DownloadData(SteamGamesAPILink);
            byte[] SteamLevelAPIResponse = wc.DownloadData(SteamLevelAPILink);


            string SteamLevelAPIResponseString = Encoding.Default.GetString(SteamLevelAPIResponse);
            string SteamGamesAPIResponseString = Encoding.Default.GetString(SteamGamesAPIResponse);

            var levelRoot = JToken.Parse(SteamLevelAPIResponseString);
            var gamesRoot = JToken.Parse(SteamGamesAPIResponseString);

#pragma warning disable CS8604
            var playtime = gamesRoot
                .SelectToken("response")
                .SelectToken("games");
                //no idea what to do next...
#pragma warning restore CS8604

            var level = levelRoot
                .SelectToken("response")
                .SelectToken("player_level");

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine($"Player level: {level}");
        }

CodePudding user response:

you can put all your code in one line using Linq

using Newtonsoft.Json;

int playTimeForEverSum = JArray.Parse(json).Sum(ja => (int) ja["playtime_forever"]);

CodePudding user response:

So. After reading some docs I went for the easiest possible solution, which looks like this:

               foreach (JToken token in playtimeRoot)
                {
                    var playtimeKeyForCurrentGame = token.SelectToken("playtime_forever").ToString();

                    totalPlaytime  = int.Parse(playtimeKeyForCurrentGame);
                }

It is working, but as I'm guessing there are lots of better ways of doing this and I'm currently working on the class method Bugus Tesa mentioned to see how it will work.

  • Related