【问题标题】:How to Serialise Dictionary<int, object> to JSON, but Ignore Key如何将 Dictionary<int, object> 序列化为 JSON,但忽略键
【发布时间】:2019-06-11 02:28:29
【问题描述】:

我有以下类属性

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",

【问题讨论】:

  • 您能提供所需的 JSON 吗?
  • 我已经更新了问题,但基本上我不希望 JSON 序列化程序忽略 Dictionary 键值。
  • 你试过选择然后序列化serialize(Hotels.select(x =&gt; x.value))
  • @Chenna 这与 OP 想要的完全相反
  • 更好的是,请提供minimal reproducible example 并提供示例输入。请务必指定您想要的确切输出

标签: c# dictionary serialization json.net


【解决方案1】:

"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"
    }
  ]
}

【讨论】:

  • 谢谢,我使用了您的答案,但返回了返回 HotelDictionary.Select(x => x.Value).ToList() 作为列表。
【解决方案2】:

您不能这样做,因为此 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();
};

【讨论】:

    【解决方案3】:

    我认为问题可能是您需要将hotel_id 添加为Hotel 对象的属性。

    根据您的数据来自哪里,这可能是个好主意,不仅仅是在这种情况下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2020-02-18
      • 2021-08-11
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多