双配额出现在json输出PHP数组json


double quotas appearing after json output in PHP array to JSON

我在

看到了一个问题的答案PHP数组json,如何摆脱一些双引号?

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

我的代码:
$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);
转换为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]]"

我不想用双引号开始和结束多个坐标

请指导/建议我如何驾驭这些双引号。

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

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

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

你的代码应该看起来像这样:

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

@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)
            )
        );