1

I have this JSON string

[ \"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

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

How can I properly deserialize the JSON string to MyEnum?

I also want to avoid using JSON.Net. I'd want to go with DataContractJsonSerializer.

4

1 回答 1

-1

你可以在这里阅读。

枚举成员值在 JSON 中被视为数字,这与它们在数据协定中的处理方式不同,在数据协定中它们被包含为成员名称

所有枚举成员都是可序列化的。如果使用,则忽略 EnumMemberAttribute 和 NonSerializedAttribute 属性。

因此,尝试为此目的使用另一个解串器。也许JSON.net或按照这里 提出的扩展 JsonSerializer 。

于 2013-06-05T07:01:51.963 回答