JSON解析

<!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" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script>
    var dog = {
        name: "Fido",
        dob: new Date(),
        legs: [1, 2, 3, 4]
    };

    var jsonstr = JSON.stringify(dog);
    console.log(jsonstr);//{"name":"Fido","dob":"2013-04-25T07:18:39.146Z","legs":[1,2,3,4]}

    var jstr = JSON.parse(jsonstr);
    console.log(jstr);// Object { name="Fido", dob="2013-04-25T07:18:39.146Z", legs=[4]}
    console.log(jstr.name);//Fido
    console.log(jstr.dob);//2013-04-25T07:18:39.146Z
    console.log(jstr.legs);//[1,2,3,4]
    console.log(jstr.legs[0]);//1
    </script>
</head>
<body>
    
</body>
</html>

 

posted @ 2013-04-25 15:22  小猩猩君  阅读(145)  评论(0编辑  收藏  举报