简体   繁体   English

结合字符串c#中的所有json路径

[英]combine all json paths in string c#

i have a large json data and i want to get all paths from the root until getting value for the paths then storing the result to a string as i described bellow here is my json for example 我有一个很大的json数据,我想从根获取所有路径,直到获取路径的值,然后将结果存储到字符串中,如我在下面描述的,这是我的json例如

{  
   "root":{  
      "first":{  
         "first1":{  
            "value":"1"
         },
         "first2":{  
            "value":"2"
         },
         "first3":{  
            "value":"3"
         }
      },
      "second":{  
         "second1":{  
            "value":"1"
         },
         "second2":{  
            "value":"2"
         },
         "second3":{  
            "value":"3"
         }
      },
      "third":{  
         "third1":{  
            "value":"1"
         },
         "third2":{  
            "value":"2"
         },
         "third3":{  
            "value":"3"
         }
      },
      "four":{  
         "value":"4"
      },
      "five":{  
         "five1":{  
            "five11":{  
               "value":"five11"
            },
            "five12":{  
               "value":"five12"
            }
         },
         "five2":{  
            "five21":{  
               "five211":{  
                  "value":"five211"
               }
            }
         }
      }
   }
}

then i want to make each paths like bellow dynamically in c# and showing in screen please tell me a way to make this 然后我想在C#中动态制作每个像波纹管一样的路径,并在屏幕上显示,请告诉我一种实现此目的的方法

root.first.first1.value
root.first.first2.value
root.first.first3.value

root.second.second1.value
......

root.four.value

root.five.five1.five11.value
root.five.five1.five12.value
....
root.five2.five21.five211.value 

Use JSON.NET and iterate recursively through the Children property and check if the current token doesn't have HasValues set to true and if that's the case add the Path property of that token to a StringBuilder or what have you. 使用JSON.NET并通过Children属性进行递归迭代,并检查当前令牌是否未将HasValues设置为true,如果是这种情况,则将该令牌的Path属性添加到StringBuilder或您拥有的令牌中。 Should give you exactly what you want. 应该给您确切您想要的。

Edith: Code Sample 伊迪丝:代码示例

I was lazy and just included the whole console application code. 我很懒,只是包括了整个控制台应用程序代码。

Example on dotnetfiddle.net dotnetfiddle.net上的示例

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

public class Program
{
    public static void Main()
    {
            var json = @"
{  
   ""root"":{  
      ""first"":{  
         ""first1"":{  
            ""value"":""1""
         },
         ""first2"":{  
            ""value"":""2""
         },
         ""first3"":{  
            ""value"":""3""
         }
      }
    }
}";

        var jobject = JObject.Parse (json);
        var sb = new StringBuilder ();

        RecursiveParse (sb, jobject);

        Console.WriteLine (sb.ToString());
    }

    public static void RecursiveParse(StringBuilder sb, JToken token)
    {
        foreach (var item in token.Children()) {
            if (item.HasValues)
            {
                RecursiveParse (sb, item);
            } else {
                sb.AppendLine (item.Path);
            }
        }

    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM