I'm using JSON.NET to de-serialize some JSON from a web service. I'm wanting to detect if a certain token is present and then act on that.
JToken token = JObject.Parse(JsonData);
I'm using the above to de-serialize t开发者_运维百科he data, i've then tried the following
if (((string)token.SelectToken("tokenname")) != null)
{
Debug.WriteLine("found");
}
else
{
Debug.WriteLine("not found");
}
each time it returns not found. Any idea's? thanks
I have been doing the following: (I'm assuming the 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
}
}
JObject obj=JObject.Parse(data);
JToken token;
if(obj.TryGetValue("tokenname", out token)) {
Debug.WriteLine(token);
}
精彩评论