C# 使用 JSON.NET 获取 JSON 值的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19892617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:04:09  来源:igfitidea点击:

Get path of JSON value using JSON.NET

c#jsonjson.net

提问by duyn9uyen

I am trying to find a path of a JSON value. Consider the following JSON:

我正在尝试查找 JSON 值的路径。考虑以下 JSON:

{
    "car": {
        "type": [{
            "sedan": {
                "make": "honda",
                "model": "civics"
            }
        },
        {
            "coupe": {
                "make": "ford",
                "model": "escort"
            }
        }]
    }
}

How can I get the path of the value "honda"? I'm looking to find something like this...

如何获得值“本田”的路径?我正在寻找这样的东西......

car_type_0_sedan_make_honda

car_type_0_sedan_make_本田

Does JSON.NET support this? I see that there is a JToken.Pathproperty but it is currently not available. http://json.codeplex.com/workitem/24136

JSON.NET 支持吗?我看到有一个JToken.Path属性,但它目前不可用。http://json.codeplex.com/workitem/24136

采纳答案by Brian Rogers

Update to the latest versionof Json.NET. The Pathproperty was added to JTokenin version 5.0 release 1(April 7, 2013).

更新到最新版本的 Json.NET。的Path属性被添加到JToken版本5.0第1版(2013年4月7日)。

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());
    }
}

Output:

输出:

car.type[0].sedan.make -> honda

回答by Sravan

you can Convert the Json to dynamic object as below.

您可以将 Json 转换为动态对象,如下所示。

JavaScriptSerializer js=new JavaScriptSerializer();
var dataObject=Json.Decode(jsonString);

Then you can reflect over it and find out your string "honda" and construct the path as you like.

然后你可以反思它并找出你的字符串“honda”并根据你的喜好构建路径。

回答by Colin Breame

You could also try the SelectTokenmethod like this:

你也可以试试这样的SelectToken方法:

var j = JObject.Parse(json);
var value = j.SelectToken("car.type[0].sedan.make");
Console.WriteLine(token.Path + " -> " + token.ToString());

Outputs:

输出:

car.type[0].sedan.make -> honda

回答by Dennis Rosenbaum

There is also a way to retrieve the path just by the value, using linq. You will need Json.NET for this.

还有一种方法可以使用 linq 仅通过值检索路径。为此,您将需要 Json.NET。

JObject jo = JObject.Parse(json);
var token = jo.Descendants()
                    .OfType<JProperty>()
                    .Where(p => p.Value.ToString() == "honda")
                    .First();

        Console.WriteLine(token.Path);

See: https://dotnetfiddle.net/vZ1zLg

请参阅:https: //dotnetfiddle.net/vZ1zLg