开发者

Detecting a token with a json object using json.net

开发者 https://www.devze.com 2023-03-18 06:04 出处:网络
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.

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消