如何使用 JsonConvert+attribute 序列化 Dictionary 以隐藏字典名称?
How to serialize Dictionary with JsonConvert+attribute in order to hide a dictionary name?
我正在尝试序列化此模型
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": {
"items": {
"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
}
我想得到这个回复
{
"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));
}
}
您的代码中的问题在于您填充字典的方式。您当前的 class 结构是:
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)
因此,为了获得所需的结果,请删除 class Lo 并将以下行直接移动到 RootObject 下:
public Dictionary<DateTime, List<Item>> items { 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; }
}
我正在尝试序列化此模型
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": {
"items": {
"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
}
我想得到这个回复
{
"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));
}
}
您的代码中的问题在于您填充字典的方式。您当前的 class 结构是:
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)
因此,为了获得所需的结果,请删除 class Lo 并将以下行直接移动到 RootObject 下:
public Dictionary<DateTime, List<Item>> items { 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; }
}