json嵌套遍历实例
json嵌套遍历实例
高处胜寒 发表于2个月前
json嵌套遍历实例
  • 发表于 2个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0
<?php


/**
 * @param $arr
 */

function p($arr)
{

    echo "<pre>";

    print_r($arr);

    echo "</pre>";
}


?>


<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>小雨在线-开发测试</title>


    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>


<div class="container-fluid" id="box">
    <!--
      <div class="chapter">
          <h3>第1章 面向对象基础</h3>
          <ul>
              <li data-media-id=""><a href="#" class="J-media-item"><i class="icon-video type"></i>面向对象介绍</a></li>
          </ul>
      </div>
    -->
</div>


<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<script>

    $(function () {
        ajaxreq();


        $(document).on("click",".J-media-item",function () {
            console.log($(this).parent().data("mediaId"));

        });


    });




    function ajaxreq() {
        $.ajax({

            //提交数据的类型 POST GET
            type: "POST",

            //提交的网址
            //url: "./test_ajax.php",
           // url: "./liti_list.json",
            url: "./t.json",

            //提交的数据
            data: {ajaxdo: "act"},

            //返回数据的格式
            datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".

            //在请求之前调用的函数
            beforeSend: function (XMLHttpRequest) {
                //todo
            },

            //成功返回之后调用的函数
            success: function (data) {

                var json_data = data;

                console.log(json_data);

                $.each(json_data,function (key,val) {

                    var jsonarr = val;
                    console.log(jsonarr);

                    for (var i=0;i<jsonarr.length;i++) {

                       var jsonobj = jsonarr[i];
                       console.log(jsonobj);
                       for (var x in jsonobj) {
                           //console.log(x);
                           console.log(jsonobj[x]);
                       }

                   }
                    
                });
                
                
                
                
                


               // console.log(data);

                var temp = "";


/*
                $.each(json_data.data, function (key, val) {

                    temp += "<div class=\"chapter\">";
                    temp += "<h3>" + val.name + "</h3>";
                    temp += "<ul>";
                    $.each(val.exams, function (k, v) {
                       // console.log(v.id + v.text);
                        temp += "<li data-media-id=\""+v.id+"\"><a href=\"#\" class=\"J-media-item\"><i class=\"icon-video type\"></i>"+v.id + v.text+"</a></li>";
                    });

                    temp += "</ul>";
                    temp += "</div>";
                    //console.log(val.name);

                });

                */

                $("#box").append(temp);


                //todo
            },

            //调用执行后调用的函数
            complete: function (XMLHttpRequest, textStatus) {
                //todo
            },

            //调用出错执行的函数
            error: function () {
                //todo
            }
        });

    }


</script>


</body>
</html>

 

{
  "programmers": [
    {
      "firstName": "Brett",
      "lastName": "McLaughlin",
      "email": "aaaa"
    },
    {
      "firstName": "Jason",
      "lastName": "Hunter",
      "email": "bbbb"
    },
    {
      "firstName": "Elliotte",
      "lastName": "Harold",
      "email": "cccc"
    }
  ],
  "authors": [
    {
      "firstName": "Isaac",
      "lastName": "Asimov",
      "genre": "science fiction"
    },
    {
      "firstName": "Tad",
      "lastName": "Williams",
      "genre": "fantasy"
    },
    {
      "firstName": "Frank",
      "lastName": "Peretti",
      "genre": "christian fiction"
    }
  ],
  "musicians": [
    {
      "firstName": "Eric",
      "lastName": "Clapton",
      "instrument": "guitar"
    },
    {
      "firstName": "Sergei",
      "lastName": "Rachmaninoff",
      "instrument": "piano"
    }
  ]
}

 

{
  "code": 0,
  "message": "成功",
  "totalCount": 0,
  "type": 0,
  "data": [
    {
      "name": "AAAA",
      "exams": [
        {
          "id": 175,
          "text": "AAA测试1",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        },
        {
          "id": 176,
          "text": "AAA测试2",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        },
        {
          "id": 177,
          "text": "AAA测试3",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        }
      ]
    },
    {
      "name": "BBBB",
      "exams": [
        {
          "id": 178,
          "text": "BBBB测试1",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        },
        {
          "id": 179,
          "text": "BBBB测试2",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        },
        {
          "id": 180,
          "text": "BBBB测试3",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        },
        {
          "id": 181,
          "text": "BBBB测试4",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        }
      ]
    },
    {
      "name": "CCCC",
      "exams": [
        {
          "id": 182,
          "text": "CCCC测试1",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        },
        {
          "id": 183,
          "text": "CCCC测试2",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        },
        {
          "id": 184,
          "text": "CCCC测试3",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        }
      ]
    },
    {
      "name": "DDDD",
      "exams": [
        {
          "id": 185,
          "text": "DDDD测试1",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        },
        {
          "id": 186,
          "text": "DDDD测试2",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        }
      ]
    },
    {
      "name": "EEEE",
      "exams": [
        {
          "id": 179,
          "text": "EEEE测试1",
          "playUrl": null,
          "summary": null,
          "speak": null,
          "analysis": null,
          "star": 6,
          "vip": 0,
          "exp": 0,
          "coin": 0
        }
      ]
    }
  ]
}

 

参考资料:

http://www.cnblogs.com/firstcsharp/p/4208956.html

https://www.zhihu.com/question/25147806

http://blog.csdn.net/a525390802/article/details/76293325?locationNum=10&fps=1

共有 人打赏支持
粉丝 5
博文 74
码字总数 53934
×
高处胜寒
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: