代码之家  ›  专栏  ›  技术社区  ›  neildt

如何将字典<int,object>序列化为json,但忽略键

  •  1
  • neildt  · 技术社区  · 6 年前

    我有以下班级的财产

    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 回复  |  直到 6 年前
        1
  •  2
  •   Anu Viswan    6 年前

    格式 "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"
        }
      ]
    }
    
        2
  •  0
  •   Camilo Terevinto Chase R Lewis    6 年前

    您不能这样做,因为此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
  •  -3
  •   Ian    6 年前

    我认为问题可能是你需要补充 hotel_id 作为 Hotel 对象。

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