php中的json数据解析问题

taodf 2015-04-03 10:39:46
$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编码后的数据被连接在一起了,然后怎么解析数据并且将他输出。
...全文
175 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
傲雪星枫 2015-04-03
  • 打赏
  • 举报
回复
先合拼再執行json_encode

$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'),
);


$arr = array();
array_push($arr, $arr1);
array_push($arr, $arr2);

$json=json_encode($arr);
echo $json;

// 解析
$data=json_decode($json, true);
echo '<pre>';
print_r($data);
echo '</pre>';

[{"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"}}]
Array
(
    [0] => Array
        (
            [4] => Array
                (
                    [g] => test
                    [b] => ssss
                )

            [2] => Array
                (
                    [g] => adaf
                    [b] => sfdsf
                )

            [8] => Array
                (
                    [g] => afasf
                    [b] => grge
                )

        )

    [1] => Array
        (
            [4] => Array
                (
                    [g] => test
                    [b] => ssss
                )

            [2] => Array
                (
                    [g] => adaf
                    [b] => sfdsf
                )

            [8] => Array
                (
                    [g] => afasf
                    [b] => grge
                )

        )

)
xuzuning 2015-04-03
  • 打赏
  • 举报
回复
$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 的博客看代码实现

21,888

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧