簡體   English   中英

如何序列化字典 <int, object> JSON,但忽略密鑰

[英]How to Serialise Dictionary<int, object> to JSON, but Ignore Key

我有以下課程財產

public Dictionary<int, Hotel> Hotels { get; set; }

當我序列化為JSON時,字典密鑰將按以下方式序列化:

{
"results": {
    "id": "d875e165-4705-459e-8532-fca2ae811ae0",
    "arrival_date": "2019-02-16",
    "departure_date": "2019-02-17",
    "expires": "2019-01-17T17:11:23.2941604+00:00",
    "hotels": {
        "9036": {
            "hotel_id": 9036,
            "name": "Beach View Hotel",
            "address": null,
            "star_rating": 0,
            "review_score": 0,
            "phone_number": null,
            "website_url": null,
            "email_address": null,
            "channels": [
                {
                    "id": 0,
                    "name": "Channel Name 0",
                    "offers": []
                }
            ]
        },
        "9049": {
            "hotel_id": 9049,
            "name": "City House Hotel",
            "address": null,
            "star_rating": 0,
            "review_score": 0,
            "phone_number": null,
            "website_url": null,
            "email_address": null,
            "channels": [
                {
                    "id": 0,
                    "name": "Channel Name 0",
                    "offers": []
                }
            ]
        },
        "9107": {
            "hotel_id": 9107,
            "name": "Park Hotel",
            "address": null,
            "star_rating": 0,
            "review_score": 0,
            "phone_number": null,
            "website_url": null,
            "email_address": null,
            "channels": [
                {
                    "id": 1,
                    "name": "Channel Name 1",
                    "offers": []
                }
            ]
        }
    },
    "errors": []
}

}

是否有可能以某種方式刪除類屬性屬性?

“ 9036”:

因此所需的JSON變為

"hotels": { "hotel_id": 9036, "name": "My Hotel Name",

格式"hotels": { "hotel_id": 9036, "name": "My Hotel Name", ... },..無效,但是可以將其設置為數組"hotels": [ { "hotel_id": 9036, "name": "My Hotel Name", ... } ]

您可以通過用JsonIgnore標記字典並公開包含旅館詞典中值的旅館集合來做到這一點。

例如,

var hotel = new Results
{
    id= "d875e165-4705-459e-8532-fca2ae811ae0",
    HotelDictionary = new Dictionary<int,Hotels> {
    [2323]=new Hotels{Id=2323,Name="Sample1"},
    [1323]=new Hotels{Id=1323,Name="Sample2"},
    }
};
var jsonString = JsonConvert.SerializeObject(hotel,Newtonsoft.Json.Formatting.Indented);

結果和酒店定義為(請注意,我忽略了其他屬性以關注詞典,但可以將其添加為最終解決方案)。

public class Results
{

    public string id { get; set; }
    [JsonIgnore]
    public Dictionary<int,Hotels> HotelDictionary { get; set; }
    [JsonProperty("hotels")]
    public IEnumerable<Hotels> Hotels => HotelDictionary.Select(x=>x.Value);
}

public class Hotels
{
    [JsonProperty("hotel_id")]
    public int Id{get;set;}
    [JsonProperty("name")]
    public string Name{get;set;}
}

輸出量

{
  "id": "d875e165-4705-459e-8532-fca2ae811ae0",
  "hotels": [
    {
      "hotel_id": 2323,
      "name": "Sample1"
    },
    {
      "hotel_id": 1323,
      "name": "Sample2"
    }
  ]
}

您不能這樣做,因為此JSON結構無效:

"hotels": { "hotel_id": 9036, "name": "My Hotel Name", ... }, { ... }

但是,您可以僅選擇值並將其序列化:

hotels.Values;

為了得到這個:

"hotels": [ { "hotel_id": 9036, "name": "My Hotel Name", ... }, { ... } ]

鑒於那是課程的一部分,您將需要一個新模型:

public class SomeName
{
    public List<Hotel> Hotels { get; set; }
}

var someName = new SomeName
{
    Hotels = hotels.Values.ToList();
};

我認為問題可能是您需要將hotel_id作為屬性添加到Hotel對象上。

無論如何,根據數據的來源,這可能是一個好主意,而不僅僅是在這種情況下。

暫無
暫無

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

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