I simulated a little bit of JSON data stored in a database table, you can simulate
{"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"}}
The following PHP file I used to execute the PDO
<?php$db = ' MySQL '; $host = ' localhost '; $port = ' 3306 '; $dbname = ' T_ Shop '; $user = ' root '; $pwd = '; $dsn = ' $db: host= $host;p ort= $port;d bname=$ DBName "; //set character set $options = array (pdo::mysql_attr_init_command=> ' 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 is a two-dimensional array containing JSON field keys//Print results 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" }} )//define an empty array put JSON into the new array after parsing the two-dimensional group $json = array () ;foreach ($row as $k + $v) { //json A new array into the id I need and json The two-dimensional array key $json [$k] [' ID '] = $v [' id ']; //to convert the JSON string with a key value to a tuple Put in the new array $json [$k] [' JSON '] =json_decode ($v [' json '],true); // Converts the JSON string //array of an array ( [3] => Array &nbsP; ( [id] => 3 [name] => sss [type] => 1 [value] => 1L,4L ) }echo json_encode ($json); exit;
Front-end Ajax receive output
<script> //parsing json function object (value) { return eval ("(" +value+ ")"); } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FUNCTION&NBSP;GET_WD () { $.get ('./json.php ',function (data) { var data = object (data) for (i in Data) { var obj = data[i][' JSON ']; var keys = []; var values = []; for (var key in OBJ) { // console.log (Key); // keys.push (key); //defines an array to accept key // values.push (Obj[key]);//Acquired value   //Append content Definition Append object id $ (' #div1 '). Append (' <p> ' +obj[key][' name ']+ ' <p> '); } } }) } </script> <title> front-end receive json</title>
This article from "Kangjunfei" blog, reproduced please contact the author!
On the problem of Ajax reading a compound JSON in a data table