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

Posted

技术标签:

【中文标题】如何将 JSON 字符串转换为 C# IEnumerable<JToken>【英文标题】:How to convert JSON string to C# IEnumerable<JToken> 【发布时间】:2021-12-25 05:08:28 【问题描述】:

所以我从网络上获取了一个 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&lt;JToken&gt; or List&lt;JToken&gt;" 像 ["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()));
    

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

【问题讨论】:

请在此处解释您为什么需要JToken,而不仅仅是正常反序列化 我只需要我能够做类似tokens.ElementAt(i).SelectToken("appid") 的事情我的想法是我可以有一个列表或JTokens 的东西,然后我可以浏览列表并从每个列表中获取所有appID。 所以你只是在 AppID 列表之后? 那种,从那个 JSON 我只需要所有 AppID 和那个 appid 的名称。 【参考方案1】:

有很多方法可以做到这一点和变化。但是,这很简单。前提是解析 -> 先选择 -> 按名称定位属性

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)

【讨论】:

【参考方案2】:

通过从使用 system.linq 从 json 字符串反序列化的对象列表中选择您想要的令牌

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

【讨论】:

嗯,对不起,我对 json 很笨,这是第一次使用它,如何反序列化字符串以获取对象列表?

以上是关于如何将 JSON 字符串转换为 C# IEnumerable<JToken>的主要内容,如果未能解决你的问题,请参考以下文章

如何将内容不同但结构相同的 JSON 字符串转换为 C# 对象?

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

琐事:如何将 JSON2.org DateTime 字符串转换为 C# DateTime

如何在 C# 中将 .json 文件转换为字符串 [重复]

C# URi 字符串太长,如何转换为 JSON raw [重复]

在C#中将对象转换为JSON字符串[重复]