• 使用 JSON.parse 反序列化 ISO 格式的日期字符串, 将返回Date格式对象


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <title>JSON.parse()</title>  
    <script type="text/javascript" src="json2.js"></script>  
    <script type="text/javascript">  
    		//json格式
        var data='{'  
        +'"root":'  
        +'['  
        +'{"name":"1","value":"0"},'  
        +'{"name":"6101","value":"西安市"},'   
        +'{"name":"6102","value":"铜川市"},'   
        +'{"name":"6103","value":"宝鸡市"},'  
        +'{"name":"6104","value":"咸阳市"},'   
        +'{"name":"6105","value":"渭南市"},'  
        +'{"name":"6106","value":"延安市"},'   
        +'{"name":"6107","value":"汉中市"},'   
        +'{"name":"6108","value":"榆林市"},'   
        +'{"name":"6109","value":"安康市"},'   
        +'{"name":"6110","value":"商洛市"}'   
        +']'  
        +'}';   
      
      
          
        //示例1:此示例使用 JSON.parse 将 JSON 字符串转换为对象  
        
        	//未解析时,json数据没有转换成对象,不能调用
        var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';  
        document.write(jsontext.surname + ", " + jsontext.firstname + ", "+ jsontext.phone) +"<br/>"; 
           //利用parse解析成对象,即可调用
        var contact = JSON.parse(jsontext);  
        document.write("json字符串未解析时:"+jsontext+"<br/>");
        document.write("json字符串通过parse解析后:"+contact+"<br/>");
        document.write(contact.surname + ", " + contact.firstname + ", "+ contact.phone);  
      
          
          
        //dateReviver  
        //var dateObj = new Date(Date.UTC('2008', +'01' - 1, +'01', +'12', +'00', +'00'))  
        //alert(dateObj.toUTCString())  
      
        //示例2:此示例使用 JSON.parse 反序列化 ISO 格式的日期字符串, 将返回Date格式对象。  
        var jsontext2 = '{ "hiredate": "2008-01-01T12:00:00Z", "birthdate": "2008-12-25T12:00:00Z" }';  
        var dates = JSON.parse(jsontext2, dateReviver);  
        document.write("<br /><br />"+dates.birthdate.toUTCString());  
        function dateReviver(key, value) {  
            var a;  
            if (typeof value === 'string') {  
                a = /^(d{4})-(d{2})-(d{2})T(d{2}):(d{2}):(d{2}(?:.d*)?)Z$/.exec(value);  
                if (a) {  
                    return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],  
                                    +a[5], +a[6]));  
                }  
            }  
            return value;  
        };  
      
    </script>  
    </head>  
    <body>  
    </body>  
    </html>  
    

      

  • 相关阅读:
    PAT 甲级 1132 Cut Integer (20 分)
    AcWing 7.混合背包问题
    AcWing 9. 分组背包问题
    AcWing 5. 多重背包问题 II
    AcWing 3. 完全背包问题
    AcWing 4. 多重背包问题
    AcWing 2. 01背包问题
    AcWing 875. 快速幂
    AcWing 874. 筛法求欧拉函数
    AcWing 873. 欧拉函数
  • 原文地址:https://www.cnblogs.com/LChenglong/p/6640773.html
Copyright © 2020-2023  润新知