How to do recursive descent of json using json.net?

前端 未结 5 998
鱼传尺愫
鱼传尺愫 2020-11-30 08:48

I am trying to parse a json file using json.net. The file looks like this

{X:
   {
      Title:\"foo\",
      xxxx:xxxx
   }
}
{Y:
   {ZZ:
        {Title: \"         


        
5条回答
  •  长情又很酷
    2020-11-30 09:25

    Try this Method I have written it after some unsuccessful tries:

     private void Traverse(JToken token, TreeNode tn)
        {
            if (token is JProperty)
                if (token.First is JValue)
                    tn.Nodes.Add(((JProperty)token).Name + ": " + ((JProperty)token).Value);
                else
                    tn = tn.Nodes.Add(((JProperty)token).Name);
    
            foreach (JToken token2 in token.Children())
                Traverse(token2, tn);
        }
    

    You first have to pass it the complete JSON file like this:

    TreeNode rooty= tvu.Nodes.Add("Rooty") // not the Indian bread,just Rooty,  Ok?
    JToken token = JToken.Parse(File.ReadAllText(<"Path to json file">));
    Traverse(token, rooty);
    

    Done, Bom you got this one: Oh no, I am not allowed to embed pictures. sad.

提交回复
热议问题