如何以json格式获取数据

时间:2018-09-08 08:05:07

标签: php json database api

当前,我正在制作一个API,我希望以JSON格式的数据。但是我无法得到它。它以正常形式出现。但是如何将其转换为JSON。如果我在循环外编写json_encode($response),那么我得到的是json格式的数据,但只有一个数据。

如果我在循环中使用json编码,那么我将获取所有数据,但不是JSON形式。如何解决这个问题。对于这个问题,我无法找到理想的解决方案。

$tsym = strtolower($_REQUEST['tsym']);
$time = strtolower($_REQUEST['time']);
$mil = $time;
$seconds = $mil / 1000;
$normal_date = date("Y-m-d H:i:s", $seconds);

$sql = "SELECT * FROM `forex` where pair='".$tsym."' and date >=  '".$normal_date."' order by date limit 0,10";

$result = mysqli_query($conn, $sql);
$response = array();


while($rows = mysqli_fetch_assoc($result)){
  $from_sym = $rows['pair'];

 //if(!isset($response[$from_sym]))
      {
           $response[$from_sym] = $rows;
          //echo json_encode($response, true);
     //}
      print_r( json_encode($response)); //this prints all the data but not 
        in json form

       }
      print_r( json_encode($response)); //this prints single data but in 
      json form

我想要所有数据,但格式为json。如何获得?感谢您的帮助。 我想要这样的数据:

{
    "CHFJPY": {
        "id": "33",
        "pair": "CHFJPY",
        "date": "2018-04-22 20:42:21",
        "price": "110.413",
        "change_rate": "0",
        "fetched": "1"
    }
},
{
    "CHFJPY": {
        "id": "75",
        "pair": "CHFJPY",
        "date": "2018-04-22 20:42:29",
        "price": "110.413",
        "change_rate": "0",
        "fetched": "1"
    }
},
{
    "CHFJPY": {
        "id": "117",
        "pair": "CHFJPY",
        "date": "2018-04-23 11:25:47",
        "price": "110.585",
        "change_rate": "0",
        "fetched": "1"
    }
},
{
    "CHFJPY": {
        "id": "159",
        "pair": "CHFJPY",
        "date": "2018-04-23 12:34:54",
        "price": "110.816",
        "change_rate": "0",
        "fetched": "1"
    }
},
{
    "CHFJPY": {
        "id": "201",
        "pair": "CHFJPY",
        "date": "2018-04-23 12:35:04",
        "price": "110.825",
        "change_rate": "0",
        "fetched": "1"
    }
}

但是我只得到一个数据。

3 个答案:

答案 0 :(得分:0)

不清楚为什么您没有json,所以..以更清晰的方式尝试

  while($rows = mysqli_fetch_assoc($result)){
      $response[$rows['pair']] = $rows;
  }
  $myJSON  =    json_encode($response);
  var_dump($myJSON);

这应该为每个“对”索引建立一个带有相关vleu的$ response数组

但是可能您需要所有行结果,所以尝试

  while($rows = mysqli_fetch_assoc($result)){
      $response[] = $rows;
  }
  $mySecondJSON  =    json_encode($response);
  var_dump($mySecondJSON); 

  while($rows = mysqli_fetch_assoc($result)){
      $response[] = $rows['pair'];
  }
  $myOtherJSON  =    json_encode($response);
  var_dump($myOtherJSON); 

答案 1 :(得分:0)

您需要将json_encode移到while循环的外部

<?php
$tsym_escaped = mysqli_real_escape_string($conn, $_REQUEST['tsym']);
$date = date("Y-m-d H:i:s", $_REQUEST['time']/1000);
$sql = sprintf(
        "SELECT * FROM `forex` WHERE `pain`='%s' AND `date`>='%s' ORDER BY `date` LIMIT 0,10",
        $tsym_escaped,
        $date
    );
$result = mysqli_query($conn, $sql);
$response = array();

while($row = mysqli_fetch_assoc($result)){
    $response[$row['pair']] = $row;
}
echo json_encode($response);

此外,将数据传递到SQL查询的方式也不安全,并且可能导致SQL注入。

答案 2 :(得分:0)

您必须在while循环中将行附加到最终数组$response

$response = array();
while($rows = mysqli_fetch_assoc($result)){
    $from_sym = $rows['pair'];
    $res[$from_sym] = $rows;
    $response[] = $res;
}
print_r(json_encode($response));