十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
自己模拟了一点json数据存放在了数据库一个表中大家可以自己模拟
创新互联主营绥江网站建设的网络公司,主营网站建设方案,成都App制作,绥江h5微信小程序开发搭建,绥江网站营销推广欢迎绥江等地区企业咨询
{"1":{"id":"1","name":"asd","type":"1","value":"1L,4L"}} {"2":{"id":"2","name":"kjf","type":"1","value":"1L,4L"}} {"3":{"id":"3","name":"sss","type":"1","value":"1L,4L"}}
下面我用的pdo执行的php文件
'SET NAMES \'UTF8\''); $pdo = new PDO($dsn,$user,$pwd,$options); $sql="select id,json from ecs_json"; $res=$pdo->query($sql); $row = $res->fetchAll(); // $row是一个含有json字段键值得二维数组 //打印结果 Array ( [0] => Array ( [id] => 1 [0] => 1 [json] => {"1":{"id":"1","name":"asd","type":"1","value":"1L,4L"}} [1] => {"1":{"id":"1","name":"asd","type":"1","value":"1L,4L"}} ) [1] => Array ( [id] => 2 [0] => 2 [json] => {"2":{"id":"2","name":"kjf","type":"1","value":"1L,4L"}} [1] => {"2":{"id":"2","name":"kjf","type":"1","value":"1L,4L"}} ) [2] => Array ( [id] => 3 [0] => 3 [json] => {"3":{"id":"3","name":"sss","type":"1","value":"1L,4L"}} [1] => {"3":{"id":"3","name":"sss","type":"1","value":"1L,4L"}} ) ) //定义一个空数组 把二维组里json解析后放进新数组中 $json = array(); foreach ($row as $k=>$v){ //json新数组中放入我需要的id 和 json 的二维数组键 $json[$k]['id'] =$v['id']; //把键值为json的json串转换成数组 放到新数组中 $json[$k]['json'] =json_decode($v['json'],true); //转换成数组的json串 //Array ( [3] => Array ( [id] => 3 [name] => sss [type] => 1 [value] => 1L,4L ) ) } echo json_encode($json); exit;
前端ajax接收输出
前端接收json 姓名