日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)JSON教程:轉(zhuǎn)換json格式的日期為Javascript對象的函數(shù)

項目中碰到了用jQuery從后臺獲取的JSON格式的日期的字符串,需要將此字符串轉(zhuǎn)換成JavaScript的日期對象。

代碼如下:

//轉(zhuǎn)換json格式的日期(如:{ServerDatetime:"\/Date(1278930470649)\/"})為Javascript的日期對象 
function ConvertJSONDateToJSDateObject(JSONDateString) { 
var date = new Date(parseInt(JSONDateString.replace("/Date(", "").replace(")/", ""), 10)); 
return date; 
} 

解決json日期格式問題的3種方法

開發(fā)中有時候需要從服務(wù)器端返回json格式的數(shù)據(jù),在后臺代碼中如果有DateTime類型的數(shù)據(jù)使用系統(tǒng)自帶的工具類序列化后將得到一個很長的數(shù)字表示日期數(shù)據(jù),如下所示:

代碼如下:

//設(shè)置服務(wù)器響應(yīng)的結(jié)果為純文本格式
            context.Response.ContentType = "text/plain";
           //學(xué)生對象集合
            List students = new List
            {
                new Student(){Name ="Tom",
                    Birthday =Convert.ToDateTime("2014-01-31 12:12:12")},
                new Student(){Name ="Rose",
                    Birthday =Convert.ToDateTime("2014-01-10 11:12:12")},
                new Student(){Name ="Mark",
                    Birthday =Convert.ToDateTime("2014-01-09 10:12:12")}
            };
            //javascript序列化器
            JavaScriptSerializer jss=new JavaScriptSerializer();
           //序列化學(xué)生集合對象得到j(luò)son字符
            string studentsJson=jss.Serialize(students);
           //將字符串響應(yīng)到客戶端
            context.Response.Write(studentsJson);
           context.Response.End();

運行結(jié)果是:

其中Tom所對應(yīng)生日“2014-01-31”變成了1391141532000,這其實是1970 年 1 月 1 日至今的毫秒數(shù);1391141532000/1000/60/60/24/365=44.11年,44+1970=2014年,按這種方法可以得出年月日時分秒和毫秒。這種格式是一種可行的表示形式但不是普通人可以看懂的友好格式,怎么讓這個格式變化?

解決辦法:

方法1:在服務(wù)器端將日期格式使用Select方法或LINQ表達式轉(zhuǎn)換后發(fā)到客戶端:

代碼如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Serialization;
namespace JsonDate1
{
    using System.Linq;
    /// 
    /// 學(xué)生類,測試用
    /// 
    public class Student
    {
        /// 
        /// 姓名
        /// 
        public String Name { get; set; }
        /// 
        /// 生日
        /// 
        public DateTime Birthday { get; set; }
    }
    /// 
    /// 返回學(xué)生集合的json字符
    /// 
    public class GetJson : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            //設(shè)置服務(wù)器響應(yīng)的結(jié)果為純文本格式
            context.Response.ContentType = "text/plain";
            //學(xué)生對象集合
            List students = new List
            {
                new Student(){Name ="Tom",Birthday =Convert.ToDateTime("2014-01-31 12:12:12")},
                new Student(){Name ="Rose",Birthday =Convert.ToDateTime("2014-01-10 11:12:12")},
                new Student(){Name ="Mark",Birthday =Convert.ToDateTime("2014-01-09 10:12:12")}
            };
            //使用Select方法重新投影對象集合將Birthday屬性轉(zhuǎn)換成一個新的屬性
            //注意屬性變化后要重新命名,并立即執(zhí)行
            var studentSet =
                students.Select
                (
                p => new { p.Name, Birthday = p.Birthday.ToString("yyyy-mm-dd") }
                ).ToList();
            //javascript序列化器
            JavaScriptSerializer jss = new JavaScriptSerializer();
            //序列化學(xué)生集合對象得到j(luò)son字符
            string studentsJson = jss.Serialize(studentSet);
            //將字符串響應(yīng)到客戶端
            context.Response.Write(studentsJson);
            context.Response.End();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Select方法重新投影對象集合將Birthday屬性轉(zhuǎn)換成一個新的屬性,注意屬性變化后要重新命名,屬性名可以相同;這里可以使用select方法也可以使用LINQ查詢表達式,也可以選擇別的方式達到相同的目的;這種辦法可以將集合中客戶端不用的屬性剔除,達到簡單優(yōu)化性能的目的。 
運行結(jié)果:


這時候的日期格式就已經(jīng)變成友好格式了,不過在javascript中這只是一個字符串。

方法二:

在javascript中將"Birthday":"\/Date(1391141532000)\/"中的字符串轉(zhuǎn)換成javascript中的日期對象,可以將Birthday這個Key所對應(yīng)的Value中的非數(shù)字字符以替換的方式刪除,到到一個數(shù)字1391141532000,然后實例化一個Date對象,將1391141532000毫秒作為參數(shù),得到一個javascript中的日期對象,代碼如下:

代碼如下:




    json日期格式處理
    
    


    

json日期格式處理

運行結(jié)果:

上的使用正則/\D/igm達到替換所有非數(shù)字的目的,\D表示非數(shù)字,igm是參數(shù),分別表示忽視(ignore)大小寫;多次、全局(global)替換;多行替換(multi-line);有一些時候還會出現(xiàn)+86的情況,只需要變換正則同樣可以達到目的。另外如果項目中反復(fù)出現(xiàn)這種需要處理日期格式的問題,可以擴展一個javascript方法,代碼如下:

代碼如下:

$(function () {
            $.getJSON("getJson.ashx", function (students) {
                $.each(students, function (index, obj) {
                  $("
  • ").html(obj.Name).appendTo("#ulStudents"); //使用正則表達式將生日屬性中的非數(shù)字(\D)刪除 //并把得到的毫秒數(shù)轉(zhuǎn)換成數(shù)字類型 var birthdayMilliseconds = parseInt(obj.Birthday.replace(/\D/igm, "")); //實例化一個新的日期格式,使用1970 年 1 月 1 日至今的毫秒數(shù)為參數(shù) var birthday = new Date(birthdayMilliseconds); $("
  • ").html(birthday.toLocaleString()).appendTo("#ulStudents"); $("
  • ").html(obj.Birthday.toDate()).appendTo("#ulStudents"); }); }); }); //在String對象中擴展一個toDate方法,可以根據(jù)要求完善 String.prototype.toDate = function () { var dateMilliseconds; if (isNaN(this)) { //使用正則表達式將日期屬性中的非數(shù)字(\D)刪除 dateMilliseconds =this.replace(/\D/igm, ""); } else { dateMilliseconds=this; } //實例化一個新的日期格式,使用1970 年 1 月 1 日至今的毫秒數(shù)為參數(shù) return new Date(parseInt(dateMilliseconds)); };
  • 上面擴展的方法toDate不一定合理,也不夠強大,可以根據(jù)需要修改。

    方法三:

    可以選擇一些第三方的json工具類,其中不乏有一些已經(jīng)對日期格式問題已處理好了的,常見的json序列化與反序列化工具庫有:

    1.fastJSON.
    2.JSON_checker.
    3.Jayrock.
    4.Json.NET - LINQ to JSON.
    5.LitJSON.
    6.JSON for .NET.
    7.JsonFx.
    8.JSONSharp.
    9.JsonExSerializer.
    10.fluent-json
    11.Manatee Json

    這里以litjson為序列化與反序列化json的工具類作示例,代碼如下:

    代碼如下:

    using System;
    using System.Collections.Generic;
    using System.Web;
    using LitJson;
    namespace JsonDate2
    {
        using System.Linq;
        /// 
        /// 學(xué)生類,測試用
        /// 
        public class Student
        {
            /// 
            /// 姓名
            /// 
            public String Name { get; set; }
            /// 
            /// 生日
            /// 
            public DateTime Birthday { get; set; }
        }
        /// 
        /// 返回學(xué)生集合的json字符
        /// 
        public class GetJson : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                //設(shè)置服務(wù)器響應(yīng)的結(jié)果為純文本格式
                context.Response.ContentType = "text/plain";
                //學(xué)生對象集合
                List students = new List
                {
                    new Student(){Name ="Tom",Birthday =Convert.ToDateTime("2014-01-31 12:12:12")},
                    new Student(){Name ="Rose",Birthday =Convert.ToDateTime("2014-01-10 11:12:12")},
                    new Student(){Name ="Mark",Birthday =Convert.ToDateTime("2014-01-09 10:12:12")}
                };
                //序列化學(xué)生集合對象得到j(luò)son字符
                string studentsJson = JsonMapper.ToJson(students);
                //將字符串響應(yīng)到客戶端
                context.Response.Write(studentsJson);
                context.Response.End();
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    運行結(jié)果如下:

    這時候的日期格式就基本正確了,只要在javascript中直接實例化日期就好了,

    var date = new Date("01/31/2014 12:12:12");
    alert(date.toLocaleString());

    客戶端的代碼如下:

    代碼如下:

    $(function () {
                $.getJSON("GetJson2.ashx", function (students) {
                    $.each(students, function (index, obj) {
                        $("
  • ").html(obj.Name).appendTo("#ulStudents"); var birthday = new Date(obj.Birthday); $("
  • ").html(birthday.toLocaleString()).appendTo("#ulStudents"); }); }); }); var date = new Date("01/31/2014 12:12:12"); alert(date.toLocaleString());

  • 新聞名稱:創(chuàng)新互聯(lián)JSON教程:轉(zhuǎn)換json格式的日期為Javascript對象的函數(shù)
    當(dāng)前鏈接:http://www.dlmjj.cn/article/cdjcdhi.html