簡體   English   中英

有效的JSON文件但結構不良? (Json.NET)

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

這是我第一次使用JSON。 我必須從沒有輸入過度的JSON文件中讀取對象(結構/格式)。 我一直在使用http://json2csharp.com/來了解可以從中獲得什么對象。 根據以下示例,此文件中的結構是否不好? 我已經更改了示例中的實際字段名稱,其他結構與我必須讀取的文件相同。 這些對象(水果)有數百種,而且還會更多。 在解析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; }
    }

,您絕對不應該使用RegExes或任何其他直接的字符串操作來使某些有效JSON發生變異。

將JSON解析為對象圖,並使用某些c#轉換對象圖以獲得所需的結構。 要執行轉換,請考慮使用System.Linq命名空間。

為什么? 使用linq這樣做更容易編寫,易於維護,並且不會因與字符串操作相關的陷阱而煩惱。 而且,現代JSON解析器已投入大量精力以使其可靠且快速。 嘗試使用解析器的特殊部分實現來執行特定任務將不會從此工作中受益。


警告

如果文件包含無效的JSON(例如,不是JSON但接近),則可能需要字符串操作,JSON解析器可能與非JSON不兼容。

我可以設想一個非常特殊的情況,即Regex可以提供性能上的好處,我必須對其進行測試。 但是,這不適用於您的情況。

如果問題中的第一個結構是動態產生的(即,名稱不斷變化的水果數量不等),則

  1. 這種結構的設計者做得不好(與此處無關)

  2. 您可以逐個令牌讀取JSON File令牌並將其解析為一些對象。 我的NewtonSoft的JSON庫很好地支持了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM