什么是JSON.NET相当于XML的XPath中的SelectNodes,的SelectSingleNode的? [英] What is the JSON.NET equivalent of XML's XPath, SelectNodes, SelectSingleNode?
问题描述
在present,我的code使用的结构的XmlDocument
来加载XML数据,然后的SelectNodes
通过重复的项目列表进行迭代。
At present, the structure of my code uses XmlDocument
to load Xml data and then SelectNodes
to iterate through a list of repeating items.
对于每一个元素,我使用 XmlNode.SelectSingleNode
来挑选出数组元素。
For each element, I am using XmlNode.SelectSingleNode
to pick out the field elements.
我现在想用JSON.NET实现与交付给我的JSON文件相同的结果。答案可能比其他JSON.net东西,只要它是C#积的。
I now want to use JSON.NET to achieve the same results with documents delivered to me as JSON. The answer can be something other than JSON.net, so long as it's C# integrable.
推荐答案
Json.NET 有SelectToken。它使用类似的DataBinder.Eval语法通过字符串前pression得到JSON:
Json.NET has SelectToken. It uses a syntax similar to DataBinder.Eval to get JSON via a string expression:
JObject o = JObject.Parse("{'People':[{'Name':'Jeff'},{'Name':'Joe'}]}");
// get name token of first person and convert to a string
string name = (string)o.SelectToken("People[0].Name");
或者,如果你想选择多个值:
Or if you wanted to select multiple values:
JObject o = JObject.Parse("{'People':[{'Name':'Jeff','Roles':['Manager', 'Admin']}]}");
// get role array token of first person and convert to a list of strings
IList<string> names = (string)o.SelectToken("People[0].Roles").Select(t => (string)t).ToList();
这篇关于什么是JSON.NET相当于XML的XPath中的SelectNodes,的SelectSingleNode的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!