douyinzha5820 2016-11-15 04:07
浏览 42
已采纳

将php转换为json文件的难度

I have been tiring to convert the PHP to JSON file but the JSON file generated is very different then normally occur. The table is present below

 rid    rname     mobile       email          address     opentiming       closetiming    menuid       type     averagecost         image
  1     abc     9876543212 sbjaca@gmail.com  fdsjdsfdnm    00:10:00          00:00:00        1         asian       120         http://gjsblog.esy.es/images/download.png
  2    abcdefc  9876543212  ajit@gmail.com    qwertym      00:00:03          00:00:04        2        chinese      120         http://gjsblog.esy.es/images/The_Table.png

The php file is present below

 //retrieve.php
<?php
include("dbconfig.php");
$result = @mysql_query("select * from Restaurants ");   

$response =array();

if(@mysql_num_rows($result)>0){

    $response['Restaurants'] = array();

    while($row=@mysql_fetch_array($result)){
        array_push($response['Restaurants'], $row);
    }
}

 if($result){
    $response['success']=1;
    $response['message']="Records Retrieved sucessfully";
 }else{
    $response['success']=0;
    $response['message']="Retrieval Failure";
 }

 echo json_encode($response);

?>

The JSON contains the data in the column twice, once before the column name and once after the column name. The JSON appears as

{
  "Restaurants": [
    {
      "0": "1",
      "rid": "1",
      "1": "abc",
      "rname": "abc",
      "2": "9876543212",
      "mobile": "9876543212",
      "3": "sbjaca@gmail.com",
      "email": "sbjaca@gmail.com",
      "4": "fdsjdsfdnm",
      "address": "fdsjdsfdnm",
      "5": "00:10:00",
      "opentiming": "00:10:00",
      "6": "00:00:00",
      "closetiming": "00:00:00",
      "7": "1",
      "menuid": "1",
      "8": "asian",
      "type": "asian",
      "9": "120",
      "averagecost": "120",
      "10": "http:\/\/gjsblog.esy.es\/images\/download.png",
      "image": "http:\/\/gjsblog.esy.es\/images\/download.png"
    },
    {
      "0": "2",
      "rid": "2",
      "1": "abcdefc",
      "rname": "abcdefc",
      "2": "9876543212",
      "mobile": "9876543212",
      "3": "sbjaca@gmail.com",
      "email": "sbjaca@gmail.com",
      "4": "fdsjdsfdnm",
      "address": "fdsjdsfdnm",
      "5": "00:00:03",
      "opentiming": "00:00:03",
      "6": "00:00:04",
      "closetiming": "00:00:04",
      "7": "2",
      "menuid": "2",
      "8": "chinese",
      "type": "chinese",
      "9": "120",
      "averagecost": "120",
      "10": "http:\/\/gjsblog.esy.es\/images\/The_Table_(restaurant)_logo.png",
      "image": "http:\/\/gjsblog.esy.es\/images\/The_Table_(restaurant)_logo.png"
    }
  ],
  "success": 1,
  "message": "Records Retrieved sucessfully"
}
  • 写回答

1条回答 默认 最新

  • dongshao6591 2016-11-15 05:15
    关注

    Change

    while($row=@mysql_fetch_array($result))
    

    To

    while($row=@mysql_fetch_array($result, MYSQL_ASSOC))
    

    Because if you just use the first statement, MYSQL_BOTH will be used as default, and make the result array like that.

    Just a suggestion, you better use MySQLi or PDO_MySQL, because mysql_fetch_array has been deprecated in PHP 5.5.0 and removed in PHP 7.0.0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题