.Net中Json相关套件的限制2010-12-29汪立Json.Net 无法序列基本型别(string, int),Asp.Net Ajax 无法正确序列日期,AjaxPro序列出我不想要的_type字串1. Json.Net 是我最常使用的序列/反序列json套件,标榜速度快,对於一对多关系的object 也都能正常运作, 己能满足我平日的需要,但前几天突然有个情况,我要序列的是一个泛型参数,该参数不一定是物object型别,有可能是string, int, datetime,Json.Net 却给我错误讯息不让我继续下去。string test = Newtonsoft.Json.JavaScriptConvert.SerializeObject("Hello World");出现错误: Token String in state Start would result in an invalid JavaScript object.2. Asp.Net Ajax 也内建了一组 Json 序列与反序列的函式库,只要有装 Asp.Net Ajax,他会在 namespace System.Web.Script.Serialization,只要加入参考 System.Web.Extensions,就可以用下面的方式使用。string str = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new Store());Store store = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Store>(str);本来是想,反正早晚会使用.Net 3.5,而3.5会内建Asp.Net Ajax,拿来用也顺理成章。可是测试了一下,他的日期型态是序列成 "/Date(1264118400000)/"字串,前端 javascript 拿到後并不能用datetime 来操作,相对的 Json.Net是序列成new Date(1264118400000)3.AjaxPro.Net 一个轻量的 Ajax Framework 也提供了 Json 的序列/反序列功能,没有前面2者的问题,但他序列出的字串比较大,多了一段__type的描述{"__type":"TestPage_JQuery_TestJSONPage+Store, App_Web_tnrikb42, Version=0.0.0.0, 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 you"d like to get the correct type instead of only getting the properties defined in the common abstract class