Get path of JSON value using JSON.NET

后端 未结 4 887
北恋
北恋 2021-01-11 12:14

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

{
    \"car\": {
        \"type\": [{
            \"sedan\": {
                \"mak         


        
4条回答
  •  温柔的废话
    2021-01-11 13:17

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

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

    See: https://dotnetfiddle.net/vZ1zLg

提交回复
热议问题