The response to the query , published in 2009, was to use the new JObject.SelectToken function, which should deliver XPath functionality. Now I am using JSON.NET 4.5 R11 and SelectToken .
But I could not find much documentation (mostly syntax) regarding the path string that should be passed to the SelectToken function.
The following code creates a Json string and on which I would like to execute the Xpath method (i.e. as far as I know SelectToken )
IList branches = new ArrayList(); IList employees = new ArrayList(); employees.Add(new { EmpId = 1, Name = "Name1" }); employees.Add(new { EmpId = 2, Name = "Name2" }); employees.Add(new { EmpId = 3, Name = "Name3" }); IList employees2 = new ArrayList(); employees2.Add(new { EmpId = 4, Name = "Name1" }); employees2.Add(new { EmpId = 5, Name = "Name5" }); branches.Add(new { BranchName = "Branch1", Employees = employees }); branches.Add(new { BranchName = "Branch2", Employees = employees }); string json = JsonConvert.SerializeObject(branches); var branchesDeserialised = JsonConvert.DeserializeAnonymousType(json, new[] { new { BranchName = "", Employees = new[] { new { EmpId = 0, Name = "" } } } }); JArray ja = JArray.Parse(json); var AllName1Tokens = ja.SelectToken(@"..Name=""Name1""");
Since I don't have class binaries, and the Json string structure is so great that it will be difficult to use dynamics. Therefore, using LINQ for objects after de-serialization is not possible. I do not want to convert the Json string to XML or any other format to make a choice. Also, I do not want to write code to analyze it.
What is the syntax of the Path parameter of the SelectToken function? How to select all EmpId from employees , where Name="Name1" ?
Edit1 : is it possible to get the result using JObject.Select (LINQ query) in the JSON string (and not on the real object) if SelectToken not able to do this? How about regex?
source share