第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
紅糖糍粑 2022-01-15 15:37:18
我有一個這樣的 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# 對象
查看完整描述

3 回答

?
蕪湖不蕪

TA貢獻1796條經(jīng)驗 獲得超7個贊

您的數(shù)據(jù)模型應(yīng)如下所示:


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應(yīng)該被命名為Items.


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


{

    "Items": {

        "Item_1A": { },

        "Item2B": { }

    }

}

這應(yīng)該建模為Dictionary<string, T>適當?shù)姆羌项愋蚑。假設(shè)您正在使用json.net有關(guān)詳細信息,請參閱序列化指南:字典。如果不,javascript序列化器 行為相似。


您的數(shù)據(jù)模型 ,public class Dictionary<string, List<Info>> Items將適用于具有可變名稱數(shù)組值屬性的對象:


{

    "Items": {

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

        "Item2B": [{ }]

    }

}

但這不是你所擁有的。


同時"prop4"是一個包含對象的數(shù)組,該對象具有可變名稱的對象值屬性,例如:


"prop4": [ // Outer container is an array

  {        // Inner container is an object

    "prop_x": 100

  },

  {

    "prop_y": 200

  }

]

List<T>因此,應(yīng)將諸如此類的集合用作外部泛型類型:


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

請參閱序列化指南:IEnumerable、列表和數(shù)組。


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


樣品工作小提琴在這里。


查看完整回答
反對 回復(fù) 2022-01-15
?
浮云間

TA貢獻1829條經(jīng)驗 獲得超4個贊

這是在 json 之后創(chuàng)建代理類的提示:

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

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

現(xiàn)在你去吧!只需將它們復(fù)制到您的模型并開始使用它。


查看完整回答
反對 回復(fù) 2022-01-15
?
小怪獸愛吃肉

TA貢獻1852條經(jīng)驗 獲得超1個贊

只是對您的模型進行了一些小改動。你prop4是一個列表或數(shù)組。


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

}


查看完整回答
反對 回復(fù) 2022-01-15
  • 3 回答
  • 0 關(guān)注
  • 236 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號