簡體   English   中英

更改文件中的格式JSON輸出?

[英]Change format JSON output in file?

我有下一個PHP代碼,該代碼使我成為一個包含SQL表中數據的JSON文件:

<?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_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);

如果要將其打印到瀏覽器中,則需要使用<pre>標簽

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

下面的代碼應該可以實現您想要的目的。 此代碼已在我的PC上進行了測試,似乎運行良好。

<?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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM