jquery解析php通過ajax傳過來的json二維數組對象


ajax獲得php傳過來的json二維數組對象,jquery解析

 

php代碼:

<?php 
$news = array(
    '武漢'=>array(1,2,3),
    '廣州'=>array('rain','shu','fruit'),
    '北京'=>array('yellow','orange','black'),
    '上海',
    '深圳'
);

echo json_encode($news);

前台頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
    $('ul').click(function(){
        $.get('29.php',function(msg){
            var i=0;
            $.each(msg,function(n,value){
                if (n.length!=1) {
                    var html='';
                    for(var x=0;x<value.length;x++){
                        html+=value[x]+',';
                        }
                    html=html.substring(0,html.length-1);
                    $('li').eq(i).html(n+'后面有'+html);    
                    

                }else{
                          $('li').eq(i).html(msg[n]+'后面沒有東西');
                }
                i++;
            });
        },'json');
    });
</script>
</html>

 

效果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM