如何使用json.net做JSON的递归下降? [英] How to do recursive descent of json using json.net?

查看:148
本文介绍了如何使用json.net做JSON的递归下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析使用json.net一个JSON文件。该文件看起来像这样

  {X:
{
标题:富,
XXXX:XXXX
}
}
{Y:
{ZZ:
{标题:酒吧,...}
}
}

我试图递归下降这种结构的处理具有title属性的所有对象。但我困惑的 JToken JProperty JContainer JValue JObject 。阅读源代码并没有给我留下更聪明,没有样品的帮助。我想沿着



线

  WalkNode(节点,动作<节点>动作)的东西
{
的foreach(在node.Children VAR孩子)
{
动作(孩子);
WalkNode(小孩);
}
}

解析()
{
WalkNode(根,N =>
{
如果(N [标题] = NULL)
{

}
})!;
}


解决方案

下面的代码应该是相当靠近你在找什么。我提出的假设,即有一个外阵列,并且该阵列可在层次结构中的任何位置。使用(如果这是不正确的,可以简化WalkNode法码了一点,但它应该工作无论哪种方式。)

 系统; 
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;

命名空间JsonRecursiveDescent
{
类节目
{
静态无效的主要(字串[] args)
{
串JSON =
@[
{
X:
{
标题:富,
XXXX:XXXX
}
},
{
Y:
{
,ZZ:
{
标题:酒吧,
XXXX:XXXX
}
}
}
];

JToken节点= JToken.Parse(JSON);

WalkNode(节点,N =>
{
JToken令牌= N [标题];
如果(标记=空&安培;!&安培;令牌.TYPE == JTokenType.String)
{
字符串标题= token.Value<串GT;();
Console.WriteLine(职称);
}
} );
}

静态无效WalkNode(JToken节点,动作< JObject>动作)
{
如果(node.Type == JTokenType.Object)
{
动作((JObject)节点);

的foreach(JProperty孩子node.Children< JProperty>())
{
WalkNode(child.Value,动作);
}
}
,否则如果(node.Type == JTokenType.Array)
{
的foreach(JToken孩子node.Children())
{
WalkNode(儿童,动作);
}
}
}

}
}


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

{X:
   {
      Title:"foo",
      xxxx:xxxx
   }
}
{Y:
   {ZZ:
        {Title: "bar",...}
    }
}

I am trying to recurse down this structure processing all objects with a Title attribute. But I am confused about JToken, JProperty, JContainer, JValue, JObject. Reading the source code has not left me much wiser and none of the samples help. I want something along the lines of

WalkNode(node, Action<Node> action)
{
    foreach(var child in node.Children)
    {
        Action(child);
        WalkNode(child);
    }
}

Parse()
{
   WalkNode(root, n=>
    {
        if(n["Title"] != null)
        {
           ...
        }
    });
}

解决方案

The code below should be pretty close to what you are looking for. I made the assumption that there is an outer array, and that arrays can appear anywhere in the hierarchy. (If this is not true, you can simplify the WalkNode method code a bit, but it should work either way.)

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

namespace JsonRecursiveDescent
{
    class Program
    {
        static void Main(string[] args)
        {
            string json =
            @"[
                {
                    ""X"":
                    {
                        ""Title"":""foo"",
                        ""xxxx"":""xxxx""
                    }
                },
                {
                    ""Y"":
                    {
                        ""ZZ"":
                        {
                            ""Title"":""bar"",
                            ""xxxx"":""xxxx""
                        }
                    }
                }
            ]";

            JToken node = JToken.Parse(json);

            WalkNode(node, n =>
            {
                JToken token = n["Title"];
                if (token != null && token.Type == JTokenType.String)
                {
                    string title = token.Value<string>();
                    Console.WriteLine(title);
                }
            });
        }

        static void WalkNode(JToken node, Action<JObject> action)
        {
            if (node.Type == JTokenType.Object)
            {
                action((JObject)node);

                foreach (JProperty child in node.Children<JProperty>())
                {
                    WalkNode(child.Value, action);
                }
            }
            else if (node.Type == JTokenType.Array)
            {
                foreach (JToken child in node.Children())
                {
                    WalkNode(child, action);
                }
            }
        }

    }
}

这篇关于如何使用json.net做JSON的递归下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆