javascript访问json数据简单入门例子

<html>
<head>
    <title>javascript访问json数据简单入门例子</title>
</head>
<body>
    <script type="text/javascript">
        
        var json; 
        
        window.onload = function()
        {
            //初始化字符串格式的json数据
            var s = "[{name:'张三',sex:'男'},{name:'李四',sex:'女'}]";
            eval("json = " + s);
            
            //测试取值
            var n = json.length;
            wr("共" + n + "条数据:");
            for(var i in json)
            {
                var obj = json[i];
                wr(obj.name + ", " + obj.sex);
            }
        }
        
        
        function wr(s)
        {
            document.write(s);
            document.write("<br />");
        }
        
    </script>

</body>
</html>

  

posted on 2012-03-01 16:49  ayao  阅读(493)  评论(0编辑  收藏  举报

导航