【问题标题】:Trying to deserialize JSON using JSON.NET and DataContractJsonSerializer fails尝试使用 JSON.NET 和 DataContractJsonSerializer 反序列化 JSON 失败
【发布时间】:2012-10-02 22:21:11
【问题描述】:

请帮忙,我卡住了。 我有一个 WCF 服务,它返回如下内容:

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

我想反序列化它,但无论我使用什么(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);

其中 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 反序列化的数组或列表等类型 目的。 JsonObjectAttribute 也可以添加到类型中来强制它 从 JSON 对象反序列化。路径“GetDataRESTResult”,第 1 行, 第 23 位。

【问题讨论】:

    标签: c# .net json json.net datacontractserializer


    【解决方案1】:

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

    您的数据是一个 JSON 对象(其中它有一个键“GetDataRESTResult”,其值为 JSON 数组)。因此,您应该反序列化的类型应该是一个对象,而不是一个集合。

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DataDC));
    DataDC pricinglist = (DataDC)serializer.ReadObject(memStream);
    

    如果您的类型 DataDC 看起来像这样,它将起作用:

    public class DataDC
    {
        public List<Keys> GetDataRESTResult { get; set; }
    }
    public class Keys
    {
        public double Key1 { get; set; }
        public int Key2 { get; set; }
        public string Key3 { get; set; }
    }
    

    【讨论】:

    • Maggie,非常感谢您的回答,不幸的是,我无法更改 DataDC,因为它是自动生成的,并且取自名为 GetDataREST() 的 WCF REST 服务方法,该方法尚未设置以裸格式发送 JSON,这就是它添加 GetDataRESTResult json 密钥的原因......你的答案非常有用,这就是我投票支持它的原因,但我仍想使用服务引用的 DC 而不是必须写我自己的每次它可能会改变......这就是为什么我选择第二个答案,我希望在这个答案中让 DataDC 轻松应用于 jObj["GetDataRESTResult"] 对象
    【解决方案2】:

    以下代码有效

    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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多