Retrieve a value among several json fields if this one is equal to another value of this json
I’m sorry if I’m asking a trivial question but I’m stuck so I’m asking for your help.
I currently have a dynamic json that I receive, and I would like that according to the value of the phone the corresponding field is retrieved.
For exemple in my foreach(var item in jsonResult),
if item["Phone"].Value = "PSTN"( or "UIFN", "TS", "RS", "TF" ) then I would like to retrieve the json field which corresponds with its value, in this case it would be "PSTN".
If anyone has an idea how I can make this happen.
Thank you in advance for your answers and your help.
I believe this post can help you.
Try this code:
JObject jObject = JObject.Parse(jsonResult); var result = (JObject)jObject["put here your top node level"]; foreach(JProperty prop in result.Properties()) { if (prop.Name == item["Phone"].Value) { var values = jObject["put here your top node level"][prop.Name].Values<string>(item["Phone"].Value); // do something } }
You could do it like this. thang.json
is a file with json you provided.
var json = File.ReadAllText("thang.json"); var deserialized = JsonConvert.DeserializeObject<dynamic>(json); if (new[] {"PSTN", "UIFN", "TS", "RS", "TF"}.Contains((string) deserialized.Phone)) { Console.WriteLine(deserialized[(string)deserialized.Phone]); }