如果我的响应有键" error“,我需要处理错误并显示警告框。
json.net中是否存在haskey方法?像这样:
var x= JObject.Parse(string_my);
if(x.HasKey["error_msg"])
MessageBox.Show("Error!")
发布于 2011-08-28 03:49:03
只需使用x["error_msg"]
即可。如果该属性不存在,则返回null
。
发布于 2011-08-28 03:48:06
JObject
实现了IDictionary<string, JToken>
,因此您可以使用:
IDictionary<string, JToken> dictionary = x;
if (dictionary.ContainsKey("error_msg"))
..。或者你可以使用TryGetValue
。它使用显式的接口实现来实现这两种方法,所以您不能在没有首先转换为IDictionary<string, JToken>
的情况下使用它们。
发布于 2018-10-27 03:34:02
在11.0.1发行版中,JObject.ContainsKey(string propertyName)
已成为公共方法
文档- https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_ContainsKey.htm
https://stackoverflow.com/questions/7216917
复制相似问题