请帮忙,我被困住了.我有一个WCF服务,返回这样的东西:
{
"GetDataRESTResult":
[
{"Key1":100.0000,"Key2":1,"Key3":"Min"},
{"Key1":100.0000,"Key2":2,"Key3":"Max"}
]
}
Run Code Online (Sandbox Code Playgroud)
我想反序列化它,但无论我使用什么(JSON.NET或DataContractJsonSerializer)我都会收到错误.使用DataContractJsonSerializer时,我使用的是以下代码:
byte[] data = Encoding.UTF8.GetBytes(e.Result);
MemoryStream memStream = new MemoryStream(data);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<DataDC>));
List<DataDC> pricinglist = (List<DataDC>)serializer.ReadObject(memStream);
Run Code Online (Sandbox Code Playgroud)
其中DataDC是我从WCF REST服务的服务引用得到的数据契约我从中获取JSON数据,而我得到的错误是InvalidCastException ...
试图使用JSON.NET我得到另一个例外,但我仍然无法弄明白,有人可以帮忙吗?
编辑这是一个JSON.NET堆栈跟踪:
无法将当前JSON对象(例如{"name":"value"})反序列化为类型'System.Collections.Generic.List`1 [MyApp.MyServiceReference.DataDC]',因为该类型需要JSON数组(例如[1, 2,3])正确反序列化.要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List.JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化.路径'GetDataRESTResult',第1行,第23位.