我目前正在使用Json.net在我的应用程序中使用json.我使用的API为枚举发送了一个特定的字符串格式,例如:
对于TemperatureType
具有值的类型的枚举fahrenheit, Celcius
json值是:
{"result":["TemperatureType_fahrenheit","TemperatureType_Celcius"]}
我想使用转换器直接管理它以获得一个IList<TemperatureType>
但也适用于其他枚举类型.
有人有想法吗?
我尝试使用自定义JsonConverter:
if (reader.TokenType == JsonToken.String && reader.Value != null)
{
string value = reader.Value.ToString();
var splitValues = value.Split('_');
if (splitValues.Length == 2)
{
var type = Type.GetType(splitValues[0]);
return Enum.Parse(type, splitValues[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是GetType属性,因为我没有指示所需类型和没有命名空间的参数