Home > Backend Development > PHP Tutorial > 浅谈json_encode用法_PHP

浅谈json_encode用法_PHP

WBOY
Release: 2016-05-31 13:13:25
Original
777 people have browsed it

1.从数据库中查询出来的数据,放在数组中

代码如下:


$query=mysql_query($SQL);
 while($row = mysql_fetch_array($query)){
        $xdata[]=$row['EventDate'];
         $ydata[]=intval($row['data']);
    }

2.将数据转成json

代码如下:


  $data_arr=array($xdata,$ydata)
  json_encode($data_arr);

3.HTML 页面中  AJAX调用数据

代码如下:


$.ajax({
   type: "Get",  
   url: "columndata.php?r=" + Math.floor(Math.random() * 1000 + 1),
   data: { 'BeginTime': "" + beginTime + "", "EndTime": "" + endTime + "" , "keyword": "" + keyword + "" },
   dataType: "text",  
   global: false, 
   async: false,  
   success: function (strReult) {
    if (strReult == "-1") { alert("fail!"); return; }
    var jsondata = eval("(" + strReult + ")");
       var xData = jsondata[0];                               
     var yData = jsondata[1];
       var namestr = jsondata[2];
   },
   error: function () {
    alert("fail!");
   }
  });

以上所述就是关于json_encode使用方法的全部内容了,希望对大家能有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template