我在
读了一个问题的答案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);
数组在转换为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]]"
我不希望双引号开始和结束多坐标。
请指导/建议我如何乘坐那些双引号。
答案 0 :(得分:0)
@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)
)
);
答案 1 :(得分:0)
问题在于:'coordinates' => array('['.$row['more_info'].']')
。
我不知道你为什么在这里添加'['
和']'
,但看起来你正在尝试手动构建JSON字符串。如果你这样做,那么是的,当你使用json_encode()
时,你会得到不需要的引号,因为json_encode()
将整个内容视为一个字符串。
目前尚不清楚$row['more_info']
包含哪些内容,但我猜它包含一个已经采用JSON格式的字符串。如果你想将它添加到输出JSON中,那么你需要做的第一件事就是将它转换回PHP数组。
您的代码应该如下所示:
'coordinates' => json_decode($row['more_info'], true)