【发布时间】:2010-12-19 14:35:51
【问题描述】:
以下类不使用System.Web.Script.Serialization.JavaScriptSerializer 反序列化(但会序列化)。
public class foo {
public KeyValuePair<string, string>? bar {get;set;}
}
当System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject 到达bar 属性时,尝试反序列化会导致System.NullReferenceException。 (注意,这是基于堆栈跟踪的推测。)
将属性类型更改为KeyValuePair<string,string> 可以解决问题,但如果可能的话,我想保留Nullable 类型。
JSON 正是您所期望的:
{"foo": {
"bar": {
"Key":"Jean-Luc",
"Value":"Picard"
}
}}
帮助?
【问题讨论】:
-
KeyValuePair
的反序列化并没有修复上面的错误,但实际上导致了对的两个空字符串。即,反序列化导致数据丢失。所以上面的JSON变成了[,]的KeyValuePair!
标签: asp.net json asp.net-ajax serialization nullable