2 に答える
Can JObject.SelectToken do the same thing the XPath can do?
What are the syntax of the Path parameter of SelectToken function?
I believe it only supports string path to a single token like "branches[0].employees[0].name"
How do I select all EmpId of employees where Name=”Name1”?
I'm not sure SelectToken
can do that and the constraints of your question rule out most common solutions.
I could not find much documentation (basically syntax) regarding the path string to be passed to SelectToken function.
Some documentation here:
The path is made up of property names and array indexes separated by periods. Array indexes can use either square or round brackets.
From author of JSON.NET:
Since Json.NET 6.0 supes up SelectToken with full support for JSONPath, an XPath like querying language for JSON.
JObject o = JObject.Parse(@"{
""Manufacturers"": [
{
""Name"": ""Acme Co"",
""Products"": [
{
""Name"": ""Anvil"",
""Price"": 50
}
]
},
{
""Name"": ""Contoso"",
""Products"": [
{
""Name"": ""Elbow Grease"",
""Price"": 99.95
},
{
""Name"": ""Headlight Fluid"",
""Price"": 4
}
]
}
]
}");
// manufacturer with the name 'Acme Co'
var acme = o.SelectToken("$.Manufacturers[?(@.Name == 'Acme Co')]");
More details on blog post