千家信息网

关于ajax读取数据表中存放复合json的问题

发表于:2024-11-30 作者:千家信息网编辑
千家信息网最后更新 2024年11月30日,自己模拟了一点json数据存放在了数据库一个表中大家可以自己模拟{"1":{"id":"1","name":"asd","type":"1","value":"1L,4L"}}{"2":{"id":"
千家信息网最后更新 2024年11月30日关于ajax读取数据表中存放复合json的问题

自己模拟了一点json数据存放在了数据库一个表中大家可以自己模拟

{"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             

姓名


0