当用户在Google地图上添加标记时,我将其保存为MySQL数据库中的JSON字符串,如下所示:
[{"k":52.908902047770255,"D":-3.427734375},{"k":56.31653672211301,"D":7.03125}]
“k”是纬度,“B”是添加标记的经度。
我还在标记之间保存折线,如下所示:
[[52.908902047770255,-3.427734375],[56.31653672211301,7.03125]]
我有一个函数,从数据库中获取所有标记和折线,将其显示回屏幕,这是我的查询:
function getMarkersByTripId($tripId)
{
if ($bdd = mysqli_connect(_BDD_HOST_, _BDD_USERNAME_, _BDD_PASSWORD_, _BDD_NAME_)) {
$sql = 'SELECT DISTINCT `markers`, `polylines` FROM `trip` WHERE `trip_id` = "'.$tripId.'"';
$req = mysqli_query($bdd, $sql);
if ($req) {
while ($row = mysqli_fetch_row($req)) {
$jsonData= array('markers'=>$row[0], 'polylines'=>$row[1]);
}
echo json_encode($jsonData);
}
else {
echo json_encode(array('status' => 'failure'));
}
}
if ($bdd) {
mysqli_close($bdd);
}
}
$ jsonData的var_dump如下所示:
array(2) {
["markers"]=>
string(79) "[{"k":52.908902047770255,"D":-3.427734375},{"k":56.31653672211301,"D":7.03125}]"
["polylines"]=>
string(63) "[[52.908902047770255,-3.427734375],[56.31653672211301,7.03125]]"
}
在我的JavaScript中,当我在做的时候:
console.log(jsonText);
形成如下:
"{"markers":"[{\"k\":52.908902047770255,\"D\":-3.427734375},{\"k\":56.31653672211301,\"D\":7.03125}]","polylines":"[[52.908902047770255,-3.427734375],[56.31653672211301,7.03125]]"}"
如您所见,它是一个包含函数getMarkerByTripId()函数返回的JSON结果的简单字符串。现在,我想将我的JSON字符串转换为这样的对象:
var jsonData = JSON.parse(jsonText);
console.log(jsonData);
它似乎有效,它显示如下:
Object { markers: "[{"k":52.908902047770255,"D":-3.427734375},{"k":56.31653672211301,"D":7.03125}]", polylines: "[[52.908902047770255,-3.427734375],[56.31653672211301,7.03125]]" }
问题是:我无法访问经度/经度(k / D JSON元素),它总是未定义,这是我在JSON.parse(jsontext)之后的做法:
console.log(jsonData['markers'].k);
它总是返回“未定义”,为了在Google地图上添加标记,我需要知道我在JSON解析时遇到了什么问题。其余的没问题,我知道如何遍历我的JSON对象以自动在地图上添加标记和折线。
我在想,当我从MySQL数据库中获取JSON字符串时,它可能会添加双引号,这可能会导致JSON解析失败。非常感谢有人能引导我找到正确的答案。
答案 0 :(得分:2)
jsonData.markers
加倍json
编码,您将首先parse
jsonData.markers
关键对象
var jsonText = {
"markers": "[{\"k\":52.908902047770255,\"D\":-3.427734375},{\"k\":56.31653672211301,\"D\":7.03125}]",
"polylines": "[[52.908902047770255,-3.427734375],[56.31653672211301,7.03125]]"
};
var markers = JSON.parse(jsonText.markers);
var polylines = JSON.parse(jsonText.polylines);
console.log('markers ', markers, ' polylines', polylines);
在php代码中:warp markers
,polylines
在while循环中使用json_decode()
函数,因为这两个值都已经是json字符串,当你再次使用json_encode
这些双编码
while ($row = mysqli_fetch_row($req)) {
$jsonData= array(
'markers'=>json_decode($row[0]),
'polylines'=>json_decode($row[1])
);
}
更新PHP代码
<?php
$arr1 = array(
"markers" => '[{"k":52.908902047770255,"D":-3.427734375},{"k":56.31653672211301,"D":7.03125}]',
"polylines" =>'[[52.908902047770255,-3.427734375],[56.31653672211301,7.03125]]'
);
echo "//your problem \n";
echo json_encode($arr1);
$arr2 = array(
"markers" => json_decode('[{"k":52.908902047770255,"D":-3.427734375},{"k":56.31653672211301,"D":7.03125}]'),
"polylines" =>json_decode('[[52.908902047770255,-3.427734375],[56.31653672211301,7.03125]]')
);
echo "\n\n//my solution\n";
echo json_encode($arr2);
//after solution markers, polylines keys wrapped with one more array so you will need to use `[0]` index in javascript
// jsonText.markers[0].k; jsonText.markers[0].d etc
// you can also do
$arr3 = array(
"markers" => array_shift(json_decode('[{"k":52.908902047770255,"D":-3.427734375},{"k":56.31653672211301,"D":7.03125}]')),
"polylines" =>array_shift(json_decode('[[52.908902047770255,-3.427734375],[56.31653672211301,7.03125]]'))
);
echo "\n\n//another one more solution my solution\n";
echo json_encode($arr3);
//now you can access values in JavaScript
// jsonText.markers.k; jsonText.markers.d etc
?>
答案 1 :(得分:0)
会员标记&#39;是一个数组,您必须指定您需要从哪个元素中获取此数组中的坐标。 尝试访问第一个以检查它是否有效:
console.log(jsonData['markers'][0].k);