您好,登錄后才能下訂單哦!
這篇文章主要介紹了Jil、json序列化和反序列化庫(kù)的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Jil很牛,比Newtonsoft.Json要快很多,且支持客戶端,此處只貼代碼:
using Jil; using System.Runtime.Serialization; [Serializable] class Employee { //[JilDirective(Name = "cid")] public int Id { get; set; } [IgnoreDataMember] public string Name { get; set; } [DataMember(Name = "kkl")] public string Address { get; set; } public Employee() { } //必須要有一個(gè)午餐的構(gòu)造函數(shù) public Employee(int EmployeeId, string Name) { this.Id = EmployeeId; this.Name = Name; } }var jsonString = string.Empty; using (var output = new StringWriter()) { JSON.Serialize(new Employee(666, "zhangsan"), output); Console.WriteLine(output); jsonString = output.ToString(); } using (var input = new StringReader(jsonString)) { //var result = JSON.DeserializeDynamic(jsonString); //var result = JSON.Deserialize<Employee>(jsonString); var result = JSON.Deserialize<Employee>(input); Console.WriteLine("id:{0},name:{1}", result.Id, result.Name); }
需要注意的是,反序列化的強(qiáng)類型對(duì)象必須要有無(wú)參的構(gòu)造函數(shù)或者只有一個(gè)參數(shù)的構(gòu)造函數(shù)。
Such a type should have one declared field or property, and default or single parameter constructor.
對(duì)于時(shí)間處理,默認(rèn)是ISO8601方式,可通過配置修改:
Options _jilOptions = new Options( dateFormat: DateTimeFormat.MillisecondsSinceUnixEpoch, includeInherited: true, serializationNameFormat: SerializationNameFormat.CamelCase ); var output = JSON.Serialize(new { UserName = "jon", TradingPassword = "123456", ClientIp = "192.168.3.1", Origin = 1, time = DateTime.Now }, _jilOptions); Console.WriteLine(output); Console.WriteLine("----------------"); var pt = "1459481266695"; //時(shí)間戳 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); //說明下,時(shí)間格式為13位后面補(bǔ)加4個(gè)"0",如果時(shí)間格式為10位則后面補(bǔ)加7個(gè)"0" long lTime = long.Parse(pt + (pt.Length == 13 ? "0000" : "0000000")); var dtResult = dtStart.Add(new TimeSpan(lTime)); //得到轉(zhuǎn)換后的時(shí)間 Console.WriteLine(dtResult); Console.WriteLine("----------------"); var _time = DateTime.Now.Ticks; Console.WriteLine(DateTime.Now.ToString()); Console.WriteLine("當(dāng)前時(shí)間轉(zhuǎn)換后模式:---------------->"); var dt = DateTime.FromBinary(_time); //635951023596206937【注意,此處與】 Console.WriteLine(dt.ToLongDateString()); //2016年4月1日 Console.WriteLine(dt.ToLongTimeString()); //10:12:39 Console.WriteLine(dt.ToShortDateString()); //2016/4/1 Console.WriteLine(dt.ToShortTimeString()); //10:12 Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss")); //2016-04-01 10:12:39
輸入如下:
關(guān)于客戶端時(shí)間戳的js處理,可參閱此文:js時(shí)間戳和c#時(shí)間戳互轉(zhuǎn)方法(推薦)
var date = new Date(1459481266695); Y = date.getFullYear() + '-'; M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'; D = date.getDate() + ' '; h = date.getHours() + ':'; m = date.getMinutes() + ':'; s = date.getSeconds(); console.log(Y+M+D+h+m+s); VM307:9 2016-04-1 11:27:46
js客戶端獲取時(shí)間戳:
var dt= new Date().getTime();
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Jil、json序列化和反序列化庫(kù)的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。