如何在JSON字符串中搜索字段? [英] How to search JSON string for fields?
本文介绍了如何在JSON字符串中搜索字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JSON字符串:[{"number":"123-456-789","array":["1", "2"]}]
.我想检查此JSON是否包含"number"
字段.我在尝试什么:
I have a JSON string: [{"number":"123-456-789","array":["1", "2"]}]
. I want check to see if this JSON contains a "number"
field. What I am trying:
string jsonString = [{"number":"123-456-789","array":["1", "2"]}];
Newtonsoft.Json.Linq.JArray jsonObject = JArray.Parse(jsonString);
然后如何在此jsonObject
中搜索"指定的字段?
How do I then "search" this jsonObject
for a specified field?
推荐答案
如果要测试"number"属性是否存在,则可以使用:
If you would like to test if the "number" property exists, then you can use:
bool exists = jsonObject[0].Children<JProperty>().Any(p => p.Name == "number");
如果要获取数字"属性的值,则可以使用
If you want to get the value of the "number" property, then you can use
string number = jsonObject[0]["number"].Value<string>();
修改 这是获取"array"属性的方法
Edit Here is how to get the "array" property
string[] arr = jsonObject[0]["array"].Values<string>().ToArray();
这篇关于如何在JSON字符串中搜索字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文