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 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳