C#解析递归json [英] C# parse recursive json

查看:293
本文介绍了C#解析递归json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON字符串解析为C#类. 但是我无法正常工作.

I am trying to parse a JSON string into a C# class. however i can't get it to work.

JSON看起来像这样

the JSON looks like this

{
    reports:
    {
        report:
        {
            id:"1"
            step:
            {
                id:"2"
                step:
                [
                    {
                        id:"3"
                        step:
                        [
                            {
                                id:"4"
                            },
                            {
                                id:"5"
                            }
                        ]
                    },
                    {
                        id:"6"
                        step:
                        {
                            id:"7"
                        }
                    }
                ]
            }
        }
    }
}

我做了一些定义结构的类.

i made a few classes to define the structure.

private class report
{
  public mongoReports reports;
}

private class mongoReports
{
  public mongoReport report;
}

private class mongoReport
{
  public string id;
  public step step;
}

private class step
{
  public string id = "";
}

private class step
{
  public string id = "";
  public step step;
}

private class step
{
  public string id = "";
  public list<step> step;
}

问题出在步骤可以具有的3个定义中. 1个没有and step属性, 1,步骤类型为步骤" 1,其中step是"step"类型的对象的列表.

the problem is in the 3 definitions that step can have. 1 without and step property, 1 with step being of type 'step' 1 with step being a list of objects of type 'step'.

如何制作适合所有这些结构的适当的阶梯"课程?

how can I make a proper 'step' class that fits all these structures?

推荐答案

如果您想了解一下,我已经在dotnetfiddle上制作了原型.

I've prototyped something on dotnetfiddle if you'd like to have a look at that.

JSON递归示例

基本上,您可以有一个根"步骤来实现步骤"列表.然后,每个步骤都有一个相关的步骤列表.然后,由SingleOrArrayConverter解析这两个列表,并根据是否存在0、1或n个步骤,将使用适当的步骤对象填充该列表. JSON反序列化程序以递归方式调用自定义转换器,因为它在JSON源中遇到了新的Step对象.

Basically you could have a 'root' step that implements lists of 'steps'. Each step then has a dependent list of steps. Both of these Lists are then parsed by the SingleOrArrayConverter and depending on whether there are 0,1 or n steps the List will get populated with the appropriate step objects. The custom converter is called recursively by the JSON deserialiser as it encounters a new Step object in the JSON source.

Class结构如下:

The Class structure would then look like:

public class Rootobject
{
    public Reports reports { get; set; }
}

public class Reports
{
    public Report report { get; set; }
}

public class Report
{
    public string id { get; set; }
    public RootStep step { get; set; }
}

public class RootStep
{
    public string id { get; set; }
    [JsonConverter(typeof(SingleOrArrayConverter<Step>))]
    public List<Step> step { get; set; }
}

public class Step
{
    public string id { get; set; }
    [JsonConverter(typeof(SingleOrArrayConverter<Step>))]
    public List<Step> step { get; set; }
}

这样做的好处是,您将直接在结果对象中处理'step'类,而不是动态对象或进一步的JSON反序列化.

The beauty of doing it this way is that you will be dealing directly with 'step' classes in your resultant object instead of dynamic objects or further JSON deserialisations.

缺点是反序列化的JSON与输入的JSON不太匹配.我不确定这是否对您来说是个问题,但是如果是这样,那么您可以考虑修改WriteJson方法来解决这个问题.

The downside is that the deserialised JSON does not quite match the input JSON. I'm not sure if that's an issue for you, but if so then you could look at modifying the WriteJson method to accommodate that.

希望有帮助.

这篇关于C#解析递归json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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