Detecting token using json object using json.net

I am using JSON.NET to de-serialize some JSON from a web service. I want to determine if some token is present, and then act on it.

JToken token = JObject.Parse(JsonData); 

I use the above to de-serialize the data, I then tried the following

 if (((string)token.SelectToken("tokenname")) != null) { Debug.WriteLine("found"); } else { Debug.WriteLine("not found"); } 

every time he returns, not found. Any ideas? thanks

+4
source share
2 answers

I did the following: (I assume JsonData is a string)

 // data is a string variable JObject obj = (JObject)JsonConvert.DeserializeObject(data); if (obj != null) { if (obj["someProperty"] != null) { // perform logic here } } 
+9
source
 JObject obj=JObject.Parse(data); JToken token; if(obj.TryGetValue("tokenname", out token)) { Debug.WriteLine(token); } 
+1
source

All Articles