首页 > 代码库 > JSON.parse

JSON.parse

把字符串为JSON格式的数据转换为Javascript对象

把javascript对象转换为JSON格式的字符串数据

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="demo"></div>
    <script> 
         var text = ‘{ "sites" : [‘ +
            ‘{ "name":"Runoob" , "url":"www.runoob.com" },‘ +
            ‘{ "name":"Google" , "url":"www.google.com" },‘ +
            ‘{ "name":"Taobao" , "url":"www.taobao.com" } ]}‘;
            console.log(text);
            //{ "sites" : [{ "name":"Runoob" , "url":"www.runoob.com" },{ "name":"Google" , "url":"www.google.com" },{ "name":"Taobao" , "url":"www.taobao.com" } ]}
            obj = JSON.parse(text);
            console.log(obj);
            //Object {sites: Array(3)}sites: Array(3)__proto__: Object
            aa = JSON.stringify(obj);
            console.log(aa);
            //{"sites":[{"name":"Runoob","url":"www.runoob.com"},{"name":"Google","url":"www.google.com"},{"name":"Taobao","url":"www.taobao.com"}]}
            document.getElementById("demo").innerHTML = obj.sites[1].name + " " + obj.sites[1].url;
    </script>
</body>
</html>

 

JSON.parse