首页 > 代码库 > json格式的基本遍历

json格式的基本遍历

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>json格式值的遍历</title>
</head>
<body>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script>
   /* $(function(){
       var date={"firstName":"John","lastName":"Mr"};
       //遍历得到"John""Mr" function 的第一个参数是对象的数量,这儿第二个参数就是"John""Mr"
        $.each(date,function(i,dom){
            alert(dom);
        })
   })*/
   /* var data=http://www.mamicode.com/{"people":[
    {
    "firstName": "张靓颖",
    "lastName":"Mrs"
    },
    {
    "firstName":"李小龙",
    "lastName":"Mr"
    }
    ]
    };
    //json 里面是多个数组的, 得到firstName值的方法, 吧firstName改为lastname 就是得到lastname的值
   $.each(data.people,function(i,dom){
       alert(dom.firstName);

   })*/
    var data={
        "name":"中国",
        "province":[{
            "name":"黑龙江",
            "cities":["哈尔滨","大庆"]
           },
            {
                "name":"广东",
                "cities":["广州","深圳","珠海"]

            },{
                "name":"台湾",
                "cities":["台北","高雄"]

            },{
              "name":"新疆",
                "cities":["乌鲁木齐"]

            }

        ]
  }
   //依照上面的列子 下面得到是“中国”和"province"对象
  /* $.each(data,function(i,dom){
       alert(dom);
   
   })*/
   //下面得到是"哈尔滨","大庆""广州","深圳","珠海""台北","高雄""乌鲁木齐"
   $.each(data.province,function(i,dom){
       alert(dom.cities);

   })


</script>
</body>
</html>

 

json格式的基本遍历