首页 >后端开发 >php教程 >怎么把JSON的多维数组转换成JS的多维数组

怎么把JSON的多维数组转换成JS的多维数组

WBOY
WBOY原创
2016-06-13 09:58:471294浏览

如何把JSON的多维数组转换成JS的多维数组

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $arr1=array("a","b","c");        $arr2=array(                array("a1","a2","a3"),                array("b1","b2","b3"),                array("c1","c2","c3")        );        $arr3=array(                 array(                    array("a11","a12","a13"),                    array("a21","a22","a23"),                    array("a31","a32","a33")                 ),                 array(                    array("b11","b12","b13"),                    array("b21","b22","b23"),                    array("b31","b32","b33")                 ),                 array(                    array("c11","c12","c13"),                    array("c21","c22","c23"),                    array("c31","c32","c33")                 )        );    $parm=$_GET["parm"];    switch ($parm){        case "arr1":            echo json_encode($arr1);            break;        case "arr2":            echo json_encode($arr2);            break;        case "arr3":            echo json_encode($arr3);            break;            }    ?>


JScript code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$.getJSON("../php/json.php?parm=arr2", function(json){//中间应该怎么写});


最终JS的如果是
JScript code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->var arr1=["a","b","c"];        var arr2=[                ["a1","a2","a3"],                ["b1","b2","b3"],                ["c1","c2","c3"]        ];        var arr3=[                 [                    ["a11","a12","a13"],                    ["a21","a22","a23"],                    ["a31","a32","a33"]                 ],                 [                    ["b11","b12","b13"],                    ["b21","b22","b23"],                    ["b31","b32","b33"]                 ],                 [                    ["c11","c12","c13"],                    ["c21","c22","c23"],                    ["c31","c32","c33"]                 ]        ];


------解决方案--------------------
对js 不是那么熟悉,所以我想知道的是竟然都传递json 给js 了,还要还原成数组做什么呢?
------解决方案--------------------
jquery 的 getJSON 方法已经将接受到得数据转换成了 json 对象数组
就是说 function(json) 中的 json 是json对象数组
你已经可以直接使用了 json.name
在通常情况下,用完就结束了。

看你的描述,似乎是要从其中构造出全局变量
你可以用 for(k in json) 遍历出其中的每个成员,使用 eval 函数将其导入全局变量中。
当然变量名你得自己构造,因为你的 php 代码没有传出
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn