Sev*_*ays 135 c# json json.net
如果我的回复有关键"错误",我需要处理错误并显示警告框.
json.net中是否存在"haskey"方法?喜欢:
var x= JObject.Parse(string_my);
if(x.HasKey["error_msg"])
MessageBox.Show("Error!")
Run Code Online (Sandbox Code Playgroud)
svi*_*ick 232
只是用x["error_msg"]
.如果该属性不存在,则返回null
.
Jon*_*eet 98
JObject
实现IDictionary<string, JToken>
,所以你可以使用:
IDictionary<string, JToken> dictionary = x;
if (dictionary.ContainsKey("error_msg"))
Run Code Online (Sandbox Code Playgroud)
......或者你可以使用TryGetValue
.它使用显式接口实现实现这两种方法,因此您无需先转换即可使用它们IDictionary<string, JToken>
.
JObject.ContainsKey(string propertyName)
在11.0.1版本中已作为公共方法
文档-https: //www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_ContainsKey.htm
归档时间: |
|
查看次数: |
88362 次 |
最近记录: |