JsonNet 无法序列基本型别(string int)AspNet Ajax 无法正确序列日期AjaxPro序列出我不想要的_type字串
JsonNet 是我最常使用的序列/反序列json套件标榜速度快对于一对多关系的object 也都能正常运作 己能满足我平日的需要但前几天突然有个情况我要序列的是一个泛型参数该参数不一定是物object型别有可能是string int datetimeJsonNet 却给我错误讯息不让我继续下去
string test = NewtonsoftJsonJavaScriptConvertSerializeObject(Hello World);
出现错误: Token String in state Start would result in an invalid JavaScript object
AspNet Ajax 也内建了一组 Json 序列与反序列的函式库只要有装 AspNet Ajax他会在 namespace SystemWebScriptSerialization只要加入参考 SystemWebExtensions就可以用下面的方式使用
string str = new SystemWebScriptSerializationJavaScriptSerializer()Serialize(new Store());
Store store = new SystemWebScriptSerializationJavaScriptSerializer()Deserialize<Store>(str);
本来是想反正早晚会使用Net 而会内建AspNet Ajax拿来用也顺理成章可是测试了一下他的日期型态是序列成 /Date()/ 字串前端 javascript 拿到后并不能用datetime 来操作相对的 JsonNet是序列成new Date()
AjaxProNet 一个轻量的 Ajax Framework 也提供了 Json 的序列/反序列功能没有前面者的问题但他序列出的字串比较大多了一段__type的描述
{__type:TestPage_JQuery_TestJSONPage+Store App_Web_tnrikb Version= Culture=neutral PublicKeyToken=null 他让我产生出来的资料多了近一倍大据作者的说法这样带来的效益是可以让使用 ArrayList的时候知道Object是什麽另外是用在有物件继承关系时可以明确取得哪一层级的物件属性原文如下:
The __type property is used to allow to use the object in ArrayLists or any other object list The next thing is that if you are using different types that are inherited from a common interface youd like to get the correct type instead of only getting the properties defined in the common abstract class