[英]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.