如何将字典<int,对象>序列化为 JSON,但忽略键



>我有以下类属性

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对象的属性。

根据数据的来源,无论如何这可能是一个好主意,而不仅仅是在这种情况下。

相关内容

  • 没有找到相关文章

最新更新