使用JSON.NET获取JSON值的路径 [英] Get path of JSON value using JSON.NET
问题描述
我正在尝试查找JSON值的路径.考虑以下JSON:
I am trying to find a path of a JSON value. Consider the following JSON:
{
"car": {
"type": [{
"sedan": {
"make": "honda",
"model": "civics"
}
},
{
"coupe": {
"make": "ford",
"model": "escort"
}
}]
}
}
如何获取值"honda"的路径?我正在寻找类似这样的东西...
How can I get the path of the value "honda"? I'm looking to find something like this...
car_type_0_sedan_make_ 本田
car_type_0_sedan_make_honda
JSON.NET是否支持此功能?我看到有一个 JToken.Path 属性,但是当前不可用. http://json.codeplex.com/workitem/24136
Does JSON.NET support this? I see that there is a JToken.Path property but it is currently not available. http://json.codeplex.com/workitem/24136
推荐答案
更新为最新版本 Json.NET. Path
属性已添加到版本5.0版本1 中的JToken
中(4月7日,2013).
Update to the latest version of Json.NET. The Path
property was added to JToken
in version 5.0 release 1 (April 7, 2013).
这是一个测试程序,可用于验证其是否有效:
Here is a test program you can use to verify that it works:
class Program
{
static void Main(string[] args)
{
string json = @"
{
""car"": {
""type"": [{
""sedan"": {
""make"": ""honda"",
""model"": ""civics""
}
},
{
""coupe"": {
""make"": ""ford"",
""model"": ""escort""
}
}]
}
}";
JObject obj = JObject.Parse(json);
JToken token = obj["car"]["type"][0]["sedan"]["make"];
Console.WriteLine(token.Path + " -> " + token.ToString());
}
}
输出:
car.type[0].sedan.make -> honda
这篇关于使用JSON.NET获取JSON值的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!