如何将Dictionary <int,object =“”>序列化为JSON,但忽略密钥

时间:2019-01-16 22:54:19

标签: c# dictionary serialization json.net

我具有以下课程属性

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

答案 2 :(得分:-3)

我认为问题可能在于您需要将hotel_id作为属性添加到Hotel对象上。

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