簡體   English   中英

將嵌套的 JSON 反序列化為 C# 類

[英]Deserialize nested JSON into C# class

我有一個這樣的 JSON 類

{
    "Items": {
        "Item_1A": {
            "prop1": "string",
            "prop2": "string",
            "prop3": 1,
            "prop4": [{
                "prop_x": 100
            },
            {
                "prop_y": 200
            }]
        },
        "Item2B": {
            "prop1": "string",
            "prop2": "string",
            "prop3": 14,
            "prop4": [{
                "prop_z": 300
            }]
        }
    }
}

我怎么能把它變成 C# 類? 這是我到目前為止所擁有的:

public class Info
{
    public string prop1 {get;set;}
    public string prop2 {get;set;}
    public int prop3 {get;set;}
    public Dictionary<string, List<int>> prop4 {get;set;}
}
public class Response
{
    public Dictionary<string, List<Info>> Item {get;set;}
}

我試圖按照這個鏈接,但沒有工作將嵌套的 JSON 反序列化為 C# 對象

這是在 json 之后創建代理類的提示:

首先,要確保它是有效的 JSON,請訪問此網站並運行驗證器: https : //jsonlint.com/

如果沒問題,有一些工具可以直接將 json 和 xml 轉換為 ac# 代理類,例如: http : //json2csharp.com/ : https://xmltocsharp.azurewebsites.net/

現在你去了! 只需將它們復制到您的模型並開始使用它。

只是對您的模型進行一些小的更改。 你的prop4是一個列表或數組。

public class Info
{
  public string prop1 {get;set;}
  public string prop2 {get;set;}
  public int prop3 {get;set;}
  public List<Dictionary<string, List<int>>> prop4 {get;set}
}
public class  Response
{
  public class Dictionary<string, Info> Items {get;set;} // Should be named Items
}

你的prop4是一個Dictionary<string, List<int>> ,序列化時看起來像這樣(為了簡潔,我在發出的 JSON 中省略了$type屬性):

{
   "X": {
      "$values": [ 1, 2 ]
   },
   "Y": {
      "$values": [ 3, 4 ]
   }    
}

您的 JSON 雖然格式良好,但“形狀”錯誤。 它看起來更像是一個包含一項的字典的數組。

在上面的 JSON 中, XY是字典中的鍵,它是一個以 JSON 建模的“常規對象”。 $values表示每個鍵的整數列表。

您的數據模型應如下所示:

public class Info
{
    public string prop1 {get;set;}
    public string prop2 {get;set;}
    public int prop3 {get;set;}
    // Modified from 
    //public Dictionary<string, List<int>> prop4 {get;set}
    public List<Dictionary<string, int>> prop4 {get;set;}
}

public class Response
{
    // Modified from 
    //public class Dictionary<string, List<Info>> Item {get;set;}
    public Dictionary<string, Info> Items {get;set;}
}

筆記:

  • Response.Item應該被命名為Items

  • 在您的 JSON 中, "Items"是一個具有可變命名的對象值屬性的對象:

     { "Items": { "Item_1A": { }, "Item2B": { } } }

    對於適當的非集合類型T Dictionary<string, T>這應該建模為Dictionary<string, T> 假設您使用的是請參閱序列化指南:字典了解詳細信息。 如果沒有, 行為類似。

    您的數據模型, public class Dictionary<string, List<Info>> Items ,將適用於具有可變命名數組值屬性的對象:

     { "Items": { "Item_1A": [{ },{ }], "Item2B": [{ }] } }

    但這不是你所擁有的。

  • 同時, "prop4"是一個包含具有可變命名對象值屬性的對象的數組,例如:

     "prop4": [ // Outer container is an array { // Inner container is an object "prop_x": 100 }, { "prop_y": 200 } ]

    因此,像List<T>這樣的集合應該用作外部泛型類型:

     public List<Dictionary<string, int>> prop4 {get;set;}

    請參閱序列化指南:IEnumerable、列表和數組

  • 如您所見,諸如如何從 JSON 對象字符串自動生成 C# 類文件中提到的代碼生成工具通常無法識別具有可變屬性名稱的 JSON 對象。 在這種情況下,自動生成的類可能需要手動替換為包含類型中適當的Dictionary<string, T>屬性。

示例工作小提琴在這里

暫無
暫無

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

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