Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Retrieve JSON Object by Containing Value

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading