在 PHP 数组中 json 输出到 JSON 之后出现双配额

double quotas appearing after json output in PHP array to JSON

我已经在

阅读了一个问题的答案

PHP Array to json, how to get rid of some double quotes?

我有同样的问题,但我的代码略有不同,所以我无法摆脱一些双引号。

我的代码:

$features = array();
$geojson = array(
    'type'      => 'FeatureCollection',
    'features'  => $features
 );
while($row = mysqli_fetch_assoc($result)) {
$type = $row['type'];
if($type == 'Point')
    {
        //$output = ;
            $feature = array(
        'type' => 'Feature',
         'properties' => array(
             'score' => "",
             'fid' => ""
        //Other fields here, end without a comma
            ),

      'geometry' => array(
        'type' => $type,
        'coordinates' => array($row['lat'], $row['lng'])
            )
        );
    }
else {
//$output = '['.$row['more_info'].']';
$feature = array(
        'type' => 'Feature',
         'properties' => array(
             'score' => "",
             'fid' => ""
        //Other fields here, end without a comma
            ),

      'geometry' => array(
        'type' => $type,
        'coordinates' => array('['.$row['more_info'].']')
            )
        );
}

    array_push($geojson['features'], $feature);
};
    mysqli_close($conn);
    echo $newgeojson =  json_encode($geojson, JSON_NUMERIC_CHECK);

array 输出转换为 json (json_encode):

Array
(
    [type] => FeatureCollection
    [features] => Array
        (
            [0] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [score] => 
                            [fid] => 
                        )

                    [geometry] => Array
                        (
                            [type] => Point
                            [coordinates] => Array
                                (
                                    [0] => 88.388786315918
                                    [1] => 22.551879205144
                                )

                        )

                )

            [1] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [score] => 
                            [fid] => 
                        )

                    [geometry] => Array
                        (
                            [type] => Polygon
                            [coordinates] => Array
                                (
                                    [0] => [[88.41796875, 22.568048815726],[88.41796875, 22.572804222704],[88.41796875, 22.577876475976],[88.428955078125, 22.578114233267],[88.428955078125, 22.573121244003],[88.428611755371, 22.568048815726],[88.41796875, 22.568048815726]]
                                )

                        )

                )

        )

)

我在 json_encode

之后得到的输出
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"score":"","fid":""},"geometry":{"type":"Point","coordinates":[88.388786315918,22.551879205144]}},{"type":"Feature","properties":{"score":"","fid":""},"geometry":{"type":"Polygon","coordinates":["[[88.41796875, 22.568048815726],[88.41796875, 22.572804222704],[88.41796875, 22.577876475976],[88.428955078125, 22.578114233267],[88.428955078125, 22.573121244003],[88.428611755371, 22.568048815726],[88.41796875, 22.568048815726]]"]}}]}

如您所见,坐标带有“ ”。

"[[88.41796875, 22.568048815726],[88.41796875, 22.572804222704],[88.41796875, 22.577876475976],[88.428955078125, 22.578114233267],[88.428955078125, 22.573121244003],[88.428611755371, 22.568048815726],[88.41796875, 22.568048815726]]"

我不想要多坐标的双引号开始和结束。

请指导/建议我如何使用那些双引号。

@Simba - 谢谢你指导我。你的向导帮助我得到准确的结果

$output = json_decode('['.$row['more_info'].']');
$feature = array(
        'type' => 'Feature',
         'properties' => array(
             'score' => "",
             'fid' => ""
        //Other fields here, end without a comma
            ),

      'geometry' => array(
        'type' => $type,
        'coordinates' => array($output)
            )
        );

问题在这里:'coordinates' => array('['.$row['more_info'].']').

我不知道您为什么要在此处添加 '['']',但看起来您正在尝试手动构建 JSON 字符串。如果你这样做,那么是的,当你使用 json_encode() 时你最终会得到不需要的引号,因为 json_encode() 将整个内容视为一个字符串。

不清楚 $row['more_info'] 开头包含什么,但我猜它包含一个已经是 JSON 格式的字符串。如果你想将它添加到你的输出 JSON,那么你需要做的第一件事就是将它转换回 PHP 数组。

您的代码可能如下所示:

'coordinates' => json_decode($row['more_info'], true)