Home > Mobile >  C# Retrieve JSON Object by Containing Value
C# Retrieve JSON Object by Containing Value

Time:11-10

I've come across a bit of a problem. I want to convert a steam game name to its corresponding steam app id via the steamapi. I'm trying to figure out how to get the entirety of a object from a value it contains (

{"applist":{"apps":[{"appid":230410,"name":"Warframe"}, {"appid":25300,"name":"Call of Duty"}, {"appid":292410,"name":"Team Fortress 2"}]}}

for example. the name here is Warframe. but I want to grab the entirety of this object just by the "name" value. I've been looking for an answer for awhile. any help would be appreciated

I tried deserializing the response to a class. but this seemed to be useless since all objects contain the same value name just with different titles

CodePudding user response:

you can try this code

var result=JObject.Parse(json)["applist"]["apps"].Where(a=> (string)a["name"]=="Warframe").FirstOrDefault();

//if you need id

var appid = (int) result["appid"];

//or in one line

int appid = (int) JObject.Parse(json)["applist"]["apps"]
.FirstOrDefault(a=> (string)a["name"]=="Warframe")?["appid"];
  • Related