首页 >后端开发 >php教程 >php中的json数据解析有关问题

php中的json数据解析有关问题

WBOY
WBOY原创
2016-06-13 12:17:57845浏览

php中的json数据解析问题
$arr1=array(
'4'=>array('g'=>'test','b'=>'ssss'),
'2'=>array('g'=>'adaf','b'=>'sfdsf'),
'8'=>array('g'=>'afasf','b'=>'grge'),
);
$arr2=array(
'4'=>array('g'=>'test','b'=>'ssss'),
'2'=>array('g'=>'adaf','b'=>'sfdsf'),
'8'=>array('g'=>'afasf','b'=>'grge'),
);

$jsonencode1=json_encode($arr1);
$jsonencode2=json_encode($arr2);

$json=$jsonencode1.$jsonencode2;
echo $json;
上面是我自己写的测试代码,问题是json编码后的数据被连接在一起了,然后怎么解析数据并且将他输出。
------解决思路----------------------

$arr1 = array(<br />  '4'=>array('g'=>'test','b'=>'ssss'),<br />  '2'=>array('g'=>'adaf','b'=>'sfdsf'),<br />  '8'=>array('g'=>'afasf','b'=>'grge'),<br />);<br />$arr2 = array(<br />  '4'=>array('g'=>'test','b'=>'ssss'),<br />  '2'=>array('g'=>'adaf','b'=>'sfdsf'),<br />  '8'=>array('g'=>'afasf','b'=>'grge'),<br />);<br />$jsonencode  = json_encode(array($arr1, $arr2));<br />echo $jsonencode;
[{"4":{"g":"test","b":"ssss"},"2":{"g":"adaf","b":"sfdsf"},"8":{"g":"afasf","b":"grge"}},{"4":{"g":"test","b":"ssss"},"2":{"g":"adaf","b":"sfdsf"},"8":{"g":"afasf","b":"grge"}}]<br />
如果你是 php5.4 及以上,可以这样美化
$jsonencode  = json_encode(array($arr1, $arr2), JSON_PRETTY_PRINT);<br />echo $jsonencode;
[<br />    {<br />        "4": {<br />            "g": "test",<br />            "b": "ssss"<br />        },<br />        "2": {<br />            "g": "adaf",<br />            "b": "sfdsf"<br />        },<br />        "8": {<br />            "g": "afasf",<br />            "b": "grge"<br />        }<br />    },<br />    {<br />        "4": {<br />            "g": "test",<br />            "b": "ssss"<br />        },<br />        "2": {<br />            "g": "adaf",<br />            "b": "sfdsf"<br />        },<br />        "8": {<br />            "g": "afasf",<br />            "b": "grge"<br />        }<br />    }<br />]<br />

否则请至 fdipzone 的博客看代码实现

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn