如何将 JSON 字符串转换为 C# IEnumerable<JToken>

How to convert JSON string to C# IEnumerable<JToken>

所以我从网络上获取了一个 json 文件到我的程序中,内容如下:

{
  "10": {
    "appid": 10,
    "name": "Counter-Strike",
    "developer": "Valve",
    "publisher": "Valve",
    "score_rank": "",
    "positive": 183964,
    "negative": 4782,
    "userscore": 0,
    "owners": "10,000,000 .. 20,000,000",
    "average_forever": 11228,
    "average_2weeks": 289,
    "median_forever": 210,
    "median_2weeks": 114,
    "price": "999",
    "initialprice": "999",
    "discount": "0",
    "ccu": 13567
  },
  "20": {
    "appid": 20,
    "name": "Team Fortress Classic",
    "developer": "Valve",
    "publisher": "Valve",
    "score_rank": "",
    "positive": 5223,
    "negative": 871,
    "userscore": 0,
    "owners": "2,000,000 .. 5,000,000",
    "average_forever": 522,
    "average_2weeks": 0,
    "median_forever": 20,
    "median_2weeks": 0,
    "price": "499",
    "initialprice": "499",
    "discount": "0",
    "ccu": 93
  },
  "30": {
    "appid": 30,
    "name": "Day of Defeat",
    "developer": "Valve",
    "publisher": "Valve",
    "score_rank": "",
    "positive": 4866,
    "negative": 543,
    "userscore": 0,
    "owners": "5,000,000 .. 10,000,000",
    "average_forever": 2191,
    "average_2weeks": 343,
    "median_forever": 24,
    "median_2weeks": 343,
    "price": "499",
    "initialprice": "499",
    "discount": "0",
    "ccu": 130
  },
  "40": {
    "appid": 40,
    "name": "Deathmatch Classic",
    "developer": "Valve",
    "publisher": "Valve",
    "score_rank": "",
    "positive": 1789,
    "negative": 400,
    "userscore": 0,
    "owners": "5,000,000 .. 10,000,000",
    "average_forever": 297,
    "average_2weeks": 0,
    "median_forever": 8,
    "median_2weeks": 0,
    "price": "499",
    "initialprice": "499",
    "discount": "0",
    "ccu": 6
  }

我正在导入它有一个字符串我怎样才能得到一个可枚举的或列表,在那里我得到所有的标记作为(Jtokens)所以“IEnumerable<JToken> or List<JToken>”像[“10”,“40”,“ 60"...]

这就是我的代码现在的样子:

string json = webClient.DownloadString("https://api.steampowered.com/ISteamApps/GetAppList/v2/");

tokens = JObject.Parse(json).Children();
//token = JObject.Parse(json).SelectToken("applist.apps");

for (int i = 0; i < tokens.Count(); i++)
{
    int currentID = (int)tokens.ElementAt(i).SelectToken("appid");

    if (SteamApps.BIsSubscribedApp(new AppId_t((uint)currentID)))
    {
        threads.Add(new Thread(new ParameterizedThreadStart(AddToDictionary)));
        threads.Last().Start(new stats(i, currentID, threads.Last()));
    }
}

但这根本不起作用,我无法读取任何值..

通过使用 system.linq

从 json 字符串反序列化的对象列表中选择您想要的标记

newlist = deserList.Select(x => x.Jtoken);

有很多方法和变体可以做到这一点。然而,这很简单。前提是 Parse -> Select First -> Target 属性 by name

var results = JToken.Parse(input)
   // Select past the dictionary
   .Select(x => x.First()) 
   // project property values in to a value tuple
   .Select(x => (Appid: x["appid"], Name: x["name"]))
   .ToArray();

foreach (var item in results)
   Console.WriteLine(item);

输出

(10, Counter-Strike)
(20, Team Fortress Classic)
(30, Day of Defeat)
(40, Deathmatch Classic)