简体   繁体   English

C#-使用DataContractJsonSerializer将JSON字符串反序列化为Enum []

[英]C# - Deserialize JSON string to Enum[] using DataContractJsonSerializer

I have this JSON string 我有这个JSON字符串

[ \"postal_code\" ]

My enum definition: 我的枚举定义:

[DataContract]
public enum MyEnum
{
    [EnumMember(Value = "postal_code")]
    PostalCode,
}

Here's what I've done so far: 到目前为止,这是我所做的:

byte[] byteArray = Encoding.ASCII.GetBytes(jsonString);
MemoryStream outputStream = new MemoryStream(byteArray);
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(MyEnum[]));
MyEnum[] myEnum = (MyEnum[]) dataContractJsonSerializer.ReadObject(outputStream);
outputStream.Close();
//return myEnum ;

I get an error upon reaching the ReadObject line 到达ReadObject行时出现错误

System.FormatException: Input string was not in the correct format: nDigits == 0.

How can I properly deserialize the JSON string to MyEnum? 如何正确将JSON字符串反序列化为MyEnum?

I also want to avoid using JSON.Net. 我也想避免使用JSON.Net。 I'd want to go with DataContractJsonSerializer. 我想和DataContractJsonSerializer一起使用。

as you can read here . 如你在这里阅读的。

Enumeration member values are treated as numbers in JSON, which is different from how they are treated in data contracts, where they are included as member names 枚举成员值在JSON中被视为数字,这与在数据协定中将其作为成员名称包括在内的方式不同

Also

All enum members are serializable. 所有枚举成员都是可序列化的。 The EnumMemberAttribute and the NonSerializedAttribute attributes are ignored if used. 如果使用EnumMemberAttribute和NonSerializedAttribute属性,则将被忽略。

So, try to use another deserializer for this purpose. 因此,尝试为此使用另一个解串器。 Maybe JSON.net or extend the JsonSerializer as proposed here . 也许JSON.net或延长JsonSerializer作为建议在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用DataContractJsonSerializer将Json反序列化为C#动态对象 - Deserialize Json into a C# dynamic object using DataContractJsonSerializer 将Json字符串反序列化为Enum C# - Deserialize Json string to Enum C# 使用C#中的DataContractJsonSerializer将子字段作为字符串反序列化JSON对象 - Deserialization of JSON object with sub field as string using DataContractJsonSerializer in C# 在不知道所有属性名称的情况下,如何在C#中反序列化JSON数据(使用DataContractJsonSerializer)? - How can you deserialize JSON data in C# (using DataContractJsonSerializer) without knowing all property names? 在C#中使用DataContractJsonSerializer反序列化JSON对象 - Deserialization of JSON object by using DataContractJsonSerializer in C# 使用DataContractJsonSerializer,将JSON字符串反序列化为具有列表和接口作为属性的C#对象 - Using DataContractJsonSerializer, deserialization of JSON string into C# object which has list & interface as properties 使用DataContractJsonSerializer使用不同类型的数组反序列化json - Deserialize json with array of different types using DataContractJsonSerializer 如何在C#中使用DataContractJsonSerializer处理此简单的JSON字符串? - How do I process this simple JSON string using DataContractJsonSerializer in C#? DataContractJsonSerializer用于反序列化JSON - DataContractJsonSerializer to deserialize JSON 在 C# 中使用 DataContractJsonSerializer 使用 List&lt;&gt; 反序列化 JSON 对象 - Deserialization of JSON object with List<> using DataContractJsonSerializer in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM