JSON_操作数据

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script >
            // JSON用途和web服务器进行数据传输
            // 数据类型不能为函数 日期 undefined
                var text='{"person":['+
                                '{"name":"bill","age":17},'+
                                '{"name":"mack","age":20}]}';
                obj=JSON.parse(text);
                console.log(obj);
            //字符串转日期
                var text1='{"birth":"2020-05-08"}';
                var obj1=JSON.parse(text1);
                console.log(obj1);
                obj1.birth=new Date(obj1.birth);
                console.log(obj1.birth);
            
            //向web发送数据时必须是字符串
            //js对象字符串化  日期会转化为字符串
                var myObj={
                    name:"lkj",
                    age:15
                };
                var myJson=JSON.stringify(myObj);
                console.log(myJson);
                    
        </script>
    </body>
</html>

 

posted @ 2022-03-24 19:17  lwx_R  阅读(10)  评论(0编辑  收藏  举报