使用 php 将 geoJSON 写入 .json 文件

Write geoJSON to .json file using php

我需要使用 php 将新的 geoJSON 功能写入 data.json 文件。现在我正在像这样将数据写入文件:

<?php
// Read from json file
$jsondata = json_decode( file_get_contents('data.json') );

// Add the new data
$jsondata []= array(
      'measure_location'=> $_POST["measure_location"],
      'measure_type'=> $_POST["measure_type"],
      'measurement'=> $_POST["measurement"], 
      'note_text'=> $_POST["note_text"]
    );

// encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
$jsondata = json_encode($jsondata, JSON_PRETTY_PRINT);

// saves the json string in "data.json" (in "dirdata" folder)
// outputs error message if data cannot be saved
if(file_put_contents('data.json', $jsondata));
?>

这就是 data.json 中的数据:

 {
    "measure_location": "52.370611247493486, 4.91587221622467",
    "measure_type": "negative",
    "measurement": "violence",
    "note_text": ""
 }

我可以调整我的 PHP 代码使数据看起来像这样吗:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [
      "4.91587221622467",
      "52.370611247493486"
    ]
  },
  "properties": {
    "type": "negative",
    "input": "violence",
    "note": ""
  }
}

感谢 charlietfl,得到了答案。将 php 代码更改为:

<?php
// Read from json file
$jsondata = json_decode( file_get_contents('data.json') );

// Add the new data
$jsondata [] = array(
                  'type' => 'Feature',
                  'geometry' => array(
                    'type' => 'Point',
                    'coordinates' => $_POST["measure_location"],
                  ),
                  'properties' => array(
                    'type' => $_POST["measure_type"],
                    'input' => $_POST["measurement"],
                    'note' => $_POST["note_text"],
                  )
                );

// encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
$jsondata = json_encode($jsondata, JSON_PRETTY_PRINT);

// saves the json string in "data.json" (in "dirdata" folder)
// outputs error message if data cannot be saved
if(file_put_contents('data.json', $jsondata));
?>