使用PHP将Mysql数据转换为JSON并返回不正确的json

时间:2019-06-18 03:20:52

标签: php json

一段时间以来,我一直在使用下面的代码返回JSON,并且它可以正常工作,但是知道我已经升级到Xcode 11,它告诉我无效的JSON。

if($response) {

    $resultArray = array();
    $tempArray = array();

    // Loop through each row in the result set
    while($row = mysqli_fetch_object($response))
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);

}else{

    echo("Error: something went wrong:  ");

    echo mysqli_error($dbc);
}

mysqli_close($dbc);

我在JSON验证器中测试了输出,它也返回了无效的JSON。我需要输出为有效的JSON。

当前输出:

[  
   {  
      "key":"2025",
      "financial_institution":"",
      "variable_rate":"0",
      "six_months":"0",
      "one_year":"0",
      "two_year":"0",
      "three_year":"0",
      "four_year":"0",
      "five_year":"0",
      "date":"2019-06-17"
   },
]

1 个答案:

答案 0 :(得分:-1)

有效的JSON如下所示,

[  
   {  
      "key":"2025",
      "financial_institution":"",
      "variable_rate":"0",
      "six_months":"0",
      "one_year":"0",
      "two_year":"0",
      "three_year":"0",
      "four_year":"0",
      "five_year":"0",
      "date":"2019-06-17"
   }
]