今天在写一个后台POST请求另一个网站上的方法时,想把从服务器接收过来的JSON字符串转换为自定义的JSON对象,用JavaScriptSerializer的Deserialize方法时,抛出如下异常:
捕捉到 System.MissingMethodException
Message=没有为类型“Shengyi.Haoyouju.Common.MyJsonResult”定义无参数的构造函数。
Source=System.Web.Extensions
StackTrace:
在 System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
在 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
在 Shengyi.Haoyouju.Kefu_WEB.Controllers.CommonController.RotatePhoto(String photoUrl, RotateFlipType rotateType) 位置 E:\SpencerCode\Source\Kefu_WEB\Kefu_WEB\Controllers\CommonController.cs:行号 846
InnerException:
最后通过重写JavaScriptConverter解决,推测是由于自定义的MyJsonResult类中的一个字段是HashTable类型,在JSON字符串反序列化成JSON对象的时候可能对这种类型的字段不能序列化。