尝试使用JSON.NET和DataContractJsonSerializer反序列化JSON失败 [英] Trying to deserialize JSON using JSON.NET and DataContractJsonSerializer fails

查看:224
本文介绍了尝试使用JSON.NET和DataContractJsonSerializer反序列化JSON失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助,我被困住了. 我有一个WCF服务,它返回的内容如下:

plz help, I'm stuck. I have a WCF service which returns something like this:

{
   "GetDataRESTResult":
     [
       {"Key1":100.0000,"Key2":1,"Key3":"Min"},
       {"Key1":100.0000,"Key2":2,"Key3":"Max"}
     ]
}

,并且我想反序列化它,但是无论使用什么(JSON.NET或DataContractJsonSerializer),我都会遇到错误. 当使用DataContractJsonSerializer时,我正在使用theis代码:

and I would like to deserialize it, but whatever I use (JSON.NET or DataContractJsonSerializer) I'm getting errors. When using DataContractJsonSerializer I'm using theis code:

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);

其中DataDC是我从WCF REST服务的服务引用中获取的数据协定,而我正在从中获取JSON数据,而我得到的错误是InvalidCastException ...

where DataDC is the data contract which I've got from the service reference of the WCF REST service I'm getting the JSON data from, and the error I'm getting is InvalidCastException...

尝试使用JSON.NET时,我又遇到了一个异常,但是我仍然找不到任何东西,有人可以帮忙吗?

Trying to use JSON.NET I get another exception, but still nothing I can figure out, can anyone help please?

编辑 这是JSON.NET堆栈跟踪:

EDIT Here's a JSON.NET stacktrace:

无法反序列化当前JSON对象(例如{"name":"value"}) 成类型 'System.Collections.Generic.List`1 [MyApp.MyServiceReference.DataDC]' 因为该类型需要JSON数组(例如[1,2,3])进行反序列化 正确地.要解决此错误,请将JSON更改为JSON数组 (例如[1,2,3])或更改反序列化类型,以使其成为常规 .NET类型(例如,不是整数之类的原始类型,不是集合 可以从JSON反序列化的类型(如数组或列表) 目的.也可以将JsonObjectAttribute添加到类型中以强制它 从JSON对象反序列化.路径"GetDataRESTResult",第1行 位置23.

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MyApp.MyServiceReference.DataDC]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'GetDataRESTResult', line 1, position 23.

推荐答案

以下代码有效

string json = @" {""GetDataRESTResult"":[{""Key1"":100.0000,""Key2"":1,""Key3"":""Min""},{""Key1"":100.0000,""Key2"":2,""Key3"":""Max""}]}";

dynamic dynObj = JsonConvert.DeserializeObject(json);
foreach (var item in dynObj.GetDataRESTResult)
{
    Console.WriteLine("{0} {1} {2}", item.Key1, item.Key3, item.Key3);
}

您也可以使用Linq

You can also use Linq

var jObj = (JObject)JsonConvert.DeserializeObject(json);
var result = jObj["GetDataRESTResult"]
                .Select(item => new
                {
                    Key1 = (double)item["Key1"],
                    Key2 = (int)item["Key2"],
                    Key3 = (string)item["Key3"],
                })
                .ToList();

这篇关于尝试使用JSON.NET和DataContractJsonSerializer反序列化JSON失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆