Look Up any node in Json.NET
AFAIK there's no XPath-like query syntax for JToken
/ JObject
, but you can make one fairly easily - see code below.
public static class StackOverflow_13033174
{
public static void Test()
{
string json = @"{
""Name"": ""Apple"",
""Expiry"": new Date(1230422400000),
""Price"": 3.99,
""ATest"": {
""MyTest"":
[
""blah"",
""blah""
]
}
}";
JObject jo = JObject.Parse(json);
JToken myTest = jo.Descendants()
.Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == "MyTest")
.Select(p => ((JProperty)p).Value)
.FirstOrDefault();
Console.WriteLine(myTest);
}
}
Here is another approach using JSONPath:
See: https://dotnetfiddle.net/EIKjnH
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
public static class StackOverflow_13033174
{
public static void Main()
{
string json = @"{
""Name"": ""Apple"",
""Expiry"": new Date(1230422400000),
""Price"": 3.99,
""ATest"": {
""MyTest"":
[
""blah"",
""blah""
]
}
}";
JObject jo = JObject.Parse(json);
JToken myTest = jo.SelectToken("*.MyTest");
Console.WriteLine(myTest);
}
}