当前位置: 代码迷 >> PHP >> php中的json数据解析有关问题
  详细解决方案

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

热度:43   发布时间:2016-04-28 17:36:28.0
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(
  '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'),
);
$jsonencode  = json_encode(array($arr1, $arr2));
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"}}]
如果你是 php5.4 及以上,可以这样美化
$jsonencode  = json_encode(array($arr1, $arr2), JSON_PRETTY_PRINT);
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"
        }
    }
]

否则请至 fdipzone 的博客看代码实现
  相关解决方案