问题描述
我有这样一个JSON:
[{
agencyId:myCity,
路:{
注:,
纬度:45.321,
LON:12.21,
streetCode: 290,
街头:street1,
fromNumber:,
toNumber:,
fromIntersection:,
toIntersection:
},
changeTypes:PARKING_BLOCK,ROAD_BLOCK],
}]
和这样的类:
公开类AlertRoad:BaseAlert
{
[JsonProperty(agencyId)]
[JsonConverter(typeof运算(StringEnumConverter))]
公共AgencyType agencyId {搞定;组; }
[JsonProperty(changeTypes)]
[JsonConverter(typeof运算(StringEnumConverter))]
公众的changetype [] {ChangeTypes获得;组; }
[JsonProperty(道)]
公共道路RoadInfo {搞定;组; }
}
AgencyType是一个枚举,并deserializiation和系列化的AgencyId工作。
的changetype为另一种枚举,但deserializiation和系列化的ChangeTypes不起作用。
我认为原因是ChangeTypes是枚举值的数组
现在的问题是:我怎么能反序列化/序列化ChangeTypes领域,还是在一般的枚举值的阵列
我试图通过定义自己的字段转换器,叫做ChangeTypeConverter和改变StrinEnumConverter到ChangeTypeConverter为ChangeTypes场,但在ReadJson起作用阅读器的值。为空
公共类ChangeTypeConverter:JsonConverter
{
公共覆盖无效WriteJson(JsonWriter作家,对象值,JsonSerializer串行)
{
}
公众覆盖对象ReadJson(JsonReader读者,类型的objectType,对象existingValue,JsonSerializer串行)
{
VAR VAL = reader.Value;
// val为null?!?
返回VAL;
}
公众覆盖布尔CanConvert(类型的objectType)
{
返回的objectType == typeof运算(一changeType);
}
}
的 StringEnumConverter
预计,只有一个枚举值。因为 ChangeTypes
是一个数组,你需要注释的财产有点不同,使其工作。
试试这个:
[JsonProperty(changeTypes ItemConverterType = typeof运算(StringEnumConverter))]
公众的changetype [] {ChangeTypes获得;组; }
I have a JSON like this:
[{
"agencyId": "myCity",
"road": {
"note": "",
"lat": "45.321",
"lon": "12.21",
"streetCode": "290",
"street": "street1",
"fromNumber": "",
"toNumber": "",
"fromIntersection": "",
"toIntersection": ""
},
"changeTypes": ["PARKING_BLOCK", "ROAD_BLOCK"],
},]
and a class like this:
public class AlertRoad : BaseAlert
{
[JsonProperty("agencyId")]
[JsonConverter(typeof(StringEnumConverter))]
public AgencyType AgencyId { get; set; }
[JsonProperty("changeTypes")]
[JsonConverter(typeof(StringEnumConverter))]
public ChangeType[] ChangeTypes { get; set; }
[JsonProperty("road")]
public Road RoadInfo { get; set; }
}
AgencyType is an enumeration, and deserializiation and serialization for AgencyId works.
ChangeType is another enumeration, but deserializiation and serialization for ChangeTypes doesn't work.I assume the reason is that ChangeTypes is an array of enumeration values.
The question is: how can I deserialize/serialize ChangeTypes field, or in general an array of enumeration values?
I tried by defining my own field converter, called ChangeTypeConverter, and changing StrinEnumConverter to ChangeTypeConverter for ChangeTypes field, but in the ReadJson function the value of reader is null.
public class ChangeTypeConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var val = reader.Value;
//val is null?!?
return val;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ChangeType);
}
}
The StringEnumConverter
expects only a single enumeration value. Because ChangeTypes
is an array, you need to annotate the property a little differently to make it work.
Try this instead:
[JsonProperty("changeTypes", ItemConverterType=typeof(StringEnumConverter))]
public ChangeType[] ChangeTypes { get; set; }
这篇关于我如何使用反序列化枚举Json.Net的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!