so i'm getting a json file from the web into my program that reads something like this:
{
"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
}
I'm importing it has a string how can I get an enumerable or list where I get all the tokens as (Jtokens) so "IEnumerable<JToken> or List<JToken>
" like ["10", "40", "60"...]
This is what my code looks like right now:
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()));
}
}
But this isn't working at all and I can't read any values..
CodePudding user response:
There are oodles of ways to do this and variations. However, this is simple enough. The premise is Parse -> Select First -> Target property 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);
Output
(10, Counter-Strike)
(20, Team Fortress Classic)
(30, Day of Defeat)
(40, Deathmatch Classic)
CodePudding user response:
by selecting the token u want from the list of objects that was deserialised from the json string using system.linq
newlist = deserList.Select(x => x.Jtoken);