`
javatoyou
  • 浏览: 1013373 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

.Net 底下,Json 相關套件的限制

 
阅读更多

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

,那...這個好功能就不能關掉嗎? 我真的暫時不需要啊..
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics