JSON 文件有效但结构错误? (Json.NET)

Valid JSON file but bad structure? (Json.NET)

这是我第一次使用 JSON。我必须从我没有输入的 JSON 文件中读取对象 (structure/format)。我一直在使用 http://json2csharp.com/ 来了解我可以从中得到什么对象。 根据下面的示例,此文件中的结构是否错误? 我已经更改了示例中的实际字段名称,其他结构与我必须读取的文件相同。这些对象(FRUITS)有数百种,而且还会更多。 在解析 JSON 文件之前,我应该使用 RegExp 来获得更好的 JSON 结构吗?

原文件:

{
"FRUITS": {
    "BANANA": {
        "category"      : "FRUITS",
        "fruit_type"    : "Peelable",
        "description"   : "Yellow soft fruit",
        "identifier"    : "BANANA",
        "attributes": [ {
                "description"   : "hardness",
                "value"         : 3
            }, {
                "description"   : "smell",
                "strength"      : 1,
                "max_value"     : 10
            }, {
                "argument"      : "PRESS",
                "description"   : "Press fruit",
                "strength"      : 2
        } ],
        "physical_type": "yellow"
    },
    "APPLE": {
        "category"      : "FRUITS",
        "fruit_type"    : "Peelable",
        "description"   : "Red hard fruit",
        "identifier"    : "APPLE",
        "attributes"    : [ {
                "description"   : "hardness",
                "value"         : 10
            }, {
                "description"   : "smell",
                "strength"      : 1,
                "max_value"     : 10
            }, {
                "argument"      : "PRESS",
                "description"   : "Press fruit",
                "strength"      : 2
        } ],
        "physical_type" : "red"
    }
}

}

类生成的是:

        public class Attribute
    {
         public string description { get; set; }
         public int value { get; set; }
         public int? strength { get; set; }
         public int? max_value { get; set; }
         public string argument { get; set; }
    }

    public class BANANA
    {
         public string category { get; set; }
         public string fruit_type { get; set; }
         public string description { get; set; }
         public string identifier { get; set; }
         public List<Attribute> attributes { get; set; }
         public string physical_type { get; set; }
    }

    public class Attribute2
    {
         public string description { get; set; }
         public int value { get; set; }
         public int? strength { get; set; }
         public int? max_value { get; set; }
         public string argument { get; set; }
    }

    public class APPLE
    {
         public string category { get; set; }
         public string fruit_type { get; set; }
         public string description { get; set; }
         public string identifier { get; set; }
         public List<Attribute2> attributes { get; set; }
         public string physical_type { get; set; }
    }

    public class FRUITS
    {
         public BANANA BANANA { get; set; }
         public APPLE APPLE { get; set; }
    }

    public class RootObject
    {
         public FRUITS FRUITS { get; set; }
    }

如果我稍微改成:

{
"FRUITS": [{        
        "category"      : "FRUITS",
        "fruit_type"    : "Peelable",
        "description"   : "Yellow soft fruit",
        "identifier"    : "BANANA",
        "attributes": [ {
                "description"   : "hardness",
                "value"         : 3
            }, {
                "description"   : "smell",
                "strength"      : 1,
                "max_value"     : 10
            }, {
                "argument"      : "PRESS",
                "description"   : "Press fruit",
                "strength"      : 2
        } ],
        "physical_type": "yellow"
    },{
        "category"      : "FRUITS",
        "fruit_type"    : "Peelable",
        "description"   : "Red hard fruit",
        "identifier"    : "APPLE",
        "attributes"    : [ {
                "description"   : "hardness",
                "value"         : 10
            }, {
                "description"   : "smell",
                "strength"      : 1,
                "max_value"     : 10
            }, {
                "argument"      : "PRESS",
                "description"   : "Press fruit",
                "strength"      : 2
            } ],
            "physical_type" : "red"
    }]  

}

我得到以下 类:

        public class Attribute
    {
         public string description { get; set; }
         public int value { get; set; }
         public int? strength { get; set; }
         public int? max_value { get; set; }
         public string argument { get; set; }
    }

    public class FRUIT
    {
         public string category { get; set; }
         public string fruit_type { get; set; }
         public string description { get; set; }
         public string identifier { get; set; }
         public List<Attribute> attributes { get; set; }
         public string physical_type { get; set; }
    }

    public class RootObject
    {
         public List<FRUIT> FRUITS { get; set; }
    }

如果您问题中的第一个结构是动态生成的(即水果的数量不断变化且名称不断变化),那么

  1. 这个结构的设计者做的不好(这里无关紧要)

  2. 您可以逐个读取 JSON 文件标记,然后 将其解析 一些对象。这很好地支持了我的 NewtonSoft 的 JSON 库。

,您绝对不应使用 RegExes 或任何其他直接字符串操作来改变某些有效的 JSON.

将 JSON 解析为对象图并使用一些 c# 转换对象图以获得所需的结构。要执行转换,请考虑使用 System.Linq 命名空间。

为什么? 使用 linq 执行此操作更易于编写、更易于维护,并且不会充满与字符串操作相关的陷阱。更重要的是,现代 JSON 解析器已投入大量精力以使其可靠且快速。尝试对解析器进行专门的部分实现以执行特定任务不会从这项工作中受益。


警告

如果文件包含无效的 JSON,(例如它不是 JSON 但接近于),则可能需要进行字符串操作,JSON 解析器可能与非 JSON.

我可以设想一些非常特殊的情况,其中 Regex 可能会提供性能优势,我必须对其进行测试。但是,这不适用于您的情况。