Heim >Backend-Entwicklung >PHP-Tutorial >php中的json数据解析有关问题
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 />