【问题标题】:Change format JSON output in file?更改文件中的 JSON 输出格式?
【发布时间】:2019-06-13 09:53:00
【问题描述】:

我有下一个 PHP 代码,它使我成为一个 JSON 文件,其中包含我的 SQL 表中的数据:

<?php
$mysqli = new mysqli('localhost','root','xxxx','xxxx');
$myArray = array();
$result = $mysqli->query("SELECT CONCAT(Datum , ' ', Tijd) as Datum, KleurL FROM metingen order by Datum ASC limit 20");
if ($result) {
  $tempArray = array();
  while ($row = $result->fetch_object()) {
    $tempArray = $row;
    array_push($myArray, $tempArray);
  }
  echo json_encode($myArray,JSON_NUMERIC_CHECK);
  $fp = fopen('resultsv2.json', 'w');
  fwrite($fp, json_encode($myArray,JSON_NUMERIC_CHECK));
  fclose($fp);
}
$result->close();
$mysqli->close();
?>

JSON 文件的输出如下所示。:

[{"Datum":"17-01-2019 10:31:39","KleurL":17.68},{"Datum":"17-01-2019 11:10:59","KleurL":71.76},{"Datum":"18-01-2019 08:40:41","KleurL":70.7},{"Datum":"18-01-2019 10:30:01","KleurL":71.03},{"Datum":"18-01-2019 12:05:46","KleurL":70.56},{"Datum":"18-01-2019 14:31:58","KleurL":16.2}]

但我希望看到该文件的输出如下所示。:

[
  [
    17.68,
    17-01-2019 10:31:39
  ],
  [
    18.11,
    17-01-2019 11:15:20
  ]
]

如何在 JSON 文件中获取此输出?

【问题讨论】:

  • 那么,基本上你是在尝试重新格式化 json 输出,对吧?
  • 如果可能的话,是的。
  • 您是否要在17-01-2019 10:31:3917.68 周围加上引号?
  • 另外,您是否希望将每个对象包装在 [ 17.68, 17-01-2019 10:31:39 ] (如您的输出)或 { 17.68, 17-01-2019 10:31 :39 }(如 JSON)?
  • 如果可能的话,我想要像最后一个布局一样的输出文件,而不是我现在拥有的。

标签: php mysql sql json file


【解决方案1】:

您可以使用json_encode() JSON_PRETTY_PRINT 选项:

json_encode($myArray,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);

编辑:

如果您使用的是 windows,则需要使用以下方式更改回车:

$myArray = array("test" => "data");

$buffer = json_encode($myArray,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);

echo str_replace("\n", "\r\n", $buffer);
// or
$fp = fopen('resultsv2.json', 'w');
fwrite($fp, str_replace("\n", "\r\n", $buffer));
fclose($fp);

如果您将其打印到浏览器中,您需要使用&lt;pre&gt; 标签

<pre>
echo json_encode($myArray,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
</pre>

【讨论】:

  • 我没有看到显示的数据有任何差异,只是在一个额外的空格之后
  • 你打印到浏览器了吗?如果将其打印到浏览器,则需要用&lt;pre&gt; 标签括起来输出。如果您在 Windows 上,则需要更改回车符,请参阅答案的编辑
【解决方案2】:

以下代码应该可以实现您想要做的事情。这段代码在我的电脑上测试过,似乎运行良好。

<?php $mysqli = new mysqli('localhost', 'root', 'xxxx', 'xxxx');
if ($result = $mysqli->query("SELECT CONCAT(Datum , ' ', Tijd) as Datum, KleurL FROM metingen order by Datum ASC limit 20")) {
    $current_row_number = 0;

    $output = "[";
    while ($row = $result->fetch_object()) {
        if ($current_row_number > 0) {
            $output = $output . ","; //add comma between each row
        }

        $output = $output . "[" . $row->KleurL . "," . $row->Datum . "]"; //add each row

        $current_row_number++;
    }
    $output = $output . "]";

    echo $output;

    $fp = fopen('resultsv2.json', 'w');
    fwrite($fp, $output);
    fclose($fp);
}

$result->close();
$mysqli->close();

如果您想很好地格式化输出,请使用以下代码:

<?php $mysqli = new mysqli('localhost', 'root', '244466666', 'tkd');
if ($result = $mysqli->query("SELECT CONCAT(Datum , ' ', Tijd) as Datum, KleurL FROM metingen order by Datum ASC limit 20")) {
    $current_row_number = 0;

    $output = "[";
    while ($row = $result->fetch_object()) {
        if ($current_row_number > 0) {
            $output = $output . ","; //add comma between each row
        }

        $output = $output . "\n\t" . "[" . "\n\t\t" . $row->KleurL . "," . "\n\t\t" . $row->Datum . "\n\t" . "]"; //add each row

        $current_row_number++;
    }
    $output = $output . "\n]";

    // echo $output; //if you want to see the output in the terminal/command prompt
    echo "<pre>" . $output . "</pre>"; //if you want to see the output in a browser

    $fp = fopen('resultsv2.json', 'w');
    fwrite($fp, $output);
    fclose($fp);
}

$result->close();
$mysqli->close();

【讨论】:

  • 谢谢,Tanmoy 它看起来更好,但是,你说的,引号必须消失,仍然是 { 而不是 [。这就是我的输出文件现在的样子。: [ { "KleurL": 17.68, "Datum": "17-01-2019 10:31:39" }, { "KleurL": 71.76, "Datum": "17- 01-2019 11:10:59" } ] 如果可能的话,KleurL 和 Datum 的名称也可以省略。
  • 好的,我去看看。
  • 不完全是。仍然看起来像这样。:{“KleurL”:16.4,“基准”:“19-01-2019 16:30:59”}但我想看到这个。:[16.4,19-01-2019 16: 30:59]
  • 我试过 echo str_replace( '{', '[', '', $output);但我没有看到任何变化。
  • 好的...我会看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多