无法从JSON反序列化可空KeyValuePair与ASP.NET AJAX [英] Can't Deserialize a Nullable KeyValuePair from JSON with ASP.NET AJAX

查看:245
本文介绍了无法从JSON反序列化可空KeyValuePair与ASP.NET AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的类不使用反序列化(但连载) System.Web.Script.Serialization.JavaScriptSerializer

The following class does not deserialize (but does serialize) using System.Web.Script.Serialization.JavaScriptSerializer.

public class foo {
  public KeyValuePair<string, string>? bar {get;set;}
}

反序列化在 System.NullReferenceException System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject 达到属性。 (请注意,这是一个基于堆栈跟踪的猜测。)

The attempt to deserialize results in a System.NullReferenceException when System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject reaches the bar property. (Note, that is a surmise based on the stack trace.)

更改属性类型为 KeyValuePair&LT;字符串,字符串&GT; 解决了这个问题,但我想,以保持可空的类型,如果在所有可能的。

Changing the property type to KeyValuePair<string,string> fixes the problem, but I'd like to keep the Nullable type if at all possible.

JSON的正是你所期望的:

The JSON is exactly what you would expect:

{"foo": {
  "bar": {
    "Key":"Jean-Luc",
    "Value":"Picard"
  }
}}

帮助?

推荐答案

发生这种情况的原因是,当的JavaScriptSerializer试图反序列化它将创建该类的一个新实例(在此KeyValuePair),然后将值赋给属性。

The reason this happens is that when the JavaScriptSerializer tries to deserialize it will create a new instance of the class (in this the KeyValuePair) and then assign the values to properties.

此将导致一个问题,因为该KeyValuePair只能分配作为构造的一部分,而不是通过属性的键和值,以便结果在一个空的键和值。

This causes an issue as the KeyValuePair can only have the key and values assigned as part of the constructor and not via properties so results in an empty key and value.

您可以通过创建实现<一类来解决这个和零问题href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptconverter.aspx\"相对=nofollow> JavaScriptConverter 和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.registerconverters.aspx\"相对=nofollow>注册这。我用下面的code处理标准KeyValuePair,但我相信你可以扩展它来处理空值。

You will be able to resolve this and the null issue by creating a class that implements JavaScriptConverter and Registering It. I have used the code below to handle a standard KeyValuePair but I am sure you can extend it to handle nulls.

public class DictionaryJavaScriptConverter<k, v> : JavaScriptConverter
{

    public override object Deserialize(System.Collections.Generic.IDictionary<string, object> dictionary, System.Type type, System.Web.Script.Serialization.JavaScriptSerializer serializer)
    {
        return new KeyValuePair<k, v>((k)dictionary["Key"], (v)dictionary["Value"]);
    }

    public override System.Collections.Generic.IDictionary<string, object> Serialize(object obj, System.Web.Script.Serialization.JavaScriptSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override System.Collections.Generic.IEnumerable<System.Type> SupportedTypes {
        get { return new System.Type[] { typeof(KeyValuePair<k, v>) }; }
    }
}

另外,你可以创建一个具有两个属性键和值的简单类。

Alternately you can create a simple class that has two properties key and value.

这篇关于无法从JSON反序列化可空KeyValuePair与ASP.NET AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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