On the problem of Ajax reading a compound JSON in a data table

Source: Internet
Author: User
Tags dsn

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             &nbsp          //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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.