C# 如何使用JsonConvert+;属性以隐藏字典名称?

C# 如何使用JsonConvert+;属性以隐藏字典名称?,c#,json.net,jsonconvert,C#,Json.net,Jsonconvert,我正在尝试序列化此模型 public class Rootobject { public Los los { get; set; } public int Id { get; set; } } public class Los { [JsonConverter(typeof(DictionaryToJsonObjectConverter))] public Dictionary<DateTime, List<

我正在尝试序列化此模型

public class Rootobject
{
    public Los los { get; set; }
    public int Id { get; set; }
}



      public class Los
        { 
[JsonConverter(typeof(DictionaryToJsonObjectConverter))]
            public Dictionary<DateTime, List<Item>> items { get; set; }
        }


public class Item
{
    public string currency { get; set; }
    public int guests { get; set; }
    public int[] price { get; set; }
}
我希望得到这样的回应

{
    "los": {
            "2020-01-10T00:00:00+01:00": [
                {
                    "currency": "EUR",
                    "guests": 1,
                    "price": [
                        443
                    ]
                }
            ],
            "2020-01-11T00:00:00+01:00": [
                {
                    "currency": "EUR",
                    "guests": 1,
                    "price": [
                        500
                    ]
                }
            ]
    },
    "Id": 1}
我想使用属性来实现这一点,但我不知道如何实现

我试着编写我的自定义json转换器,但效果不太好

 public class DictionaryToJsonObjectConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(IDictionary<DateTime, List<Item>>).IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteRawValue(JsonConvert.SerializeObject(value, Formatting.Indented));
        }
    }
public类DictionaryToJsonObjectConverter:JsonConverter
{
公共覆盖布尔CanConvert(类型objectType)
{
返回类型(IDictionary).IsAssignableFrom(objectType);
}
公共重写对象ReadJson(JsonReader阅读器,类型objectType,对象existingValue,JsonSerializer序列化程序)
{
抛出新的NotImplementedException();
}
公共重写void WriteJson(JsonWriter编写器、对象值、JsonSerializer序列化器)
{
WriteRawValue(JsonConvert.SerializeObject(value,Formatting.Indented));
}
}

代码中的问题是如何填充字典。您当前的班级结构是:

RootObject
|
|
|--los (with json proerty set to display)
    |
    |
    |--- items dictionary
您需要的是:

RootObject
|
|
|-- items dictionary los (either rename the property name to los or use JsonProperty to use los)
因此,为了获得所需的结果,请移动类Los并将下面的行直接移动到RootObject下:

public Dictionary<DateTime, List<Item>> items { get; set; }
公共字典项{get;set;}
并将项目重命名为los

因此,在更改之后,您的Rootobject对象如下所示:

public class Rootobject
{
    // public Los los { get; set; }
    // I don't think you need this converter unless you have special logic   
    [JsonConverter(typeof(DictionaryToJsonObjectConverter))]
    public Dictionary<DateTime, List<Item>> los { get; set; }
    public int Id { get; set; }
}
公共类根对象
{
//公共Los{get;set;}
//我认为你不需要这个转换器,除非你有特殊的逻辑
[JsonConverter(类型(DictionaryToJSONObject Converter))]
公共字典los{get;set;}
公共int Id{get;set;}
}

如果您编写了自定义json转换器和/或定义了用于序列化的自定义设置,是否可以向我们展示用于序列化的代码。您需要额外的
Los
对象有什么特殊原因?为什么你不能在
Rootobject
上添加
public Dictionary los
property我已经更新了我的问题实际上我通过删除自定义json转换器并添加[JsonProperty(“los”)]public Dictionary items{get;set;}解决了这个问题(typeof(DictionaryToJsonObjectConverter))]公共字典项{get;set;}
public class Rootobject
{
    // public Los los { get; set; }
    // I don't think you need this converter unless you have special logic   
    [JsonConverter(typeof(DictionaryToJsonObjectConverter))]
    public Dictionary<DateTime, List<Item>> los { get; set; }
    public int Id { get; set; }
}