【发布时间】:2016-06-25 03:16:41
【问题描述】:
我有一个 MySQL 数据库,其中包含一个包含相关数据的纬度和经度表。我正在尝试创建一个 Google Map 并希望使用 JavaScript 解析 JSON 字符串。我看过这里和 YouTube,但我不知道如何解决我做错的事情。
这是我通过 echo 语句得到的 JSON 字符串:
{"cm_mapTABLE":[["1","Angels Rest Photos OR","Angels_Rest_Photos_OR","663","aaj.jpg","2","Angel's Rest","Hike one of the trails in the Gorge with a scenic overlook, stream, and waterfall.","0","blue","4.5","45.5603","-122.171","http:\/\/www.eyehike.com\/pgallery\/index.php?\/category\/6","Angels_Rest_Photos_OR\/aae.thumb.jpg\" ALIGN=left HEIGHT=115 WIDTH=150>","http:\/\/www.eyehike.com\/pgallery\/i.php?\/galleries\/Angels_Rest_Photos_OR\/aaj-th.jpg","","","",""],["2","Ape Canyon Photos WA","Ape_Canyon_Photos_WA","681","PICT0114.jpg","3","Ape Canyon Trail","This trail is popular with hikers and mountain bikers with great views of several mountains.","0","blue","11","46.1653","-122.092","http:\/\/www.eyehike.com\/pgallery\/index.php?\/category\/8","Ape_Canyon_Photos_WA\/PICT0114.thumb.jpg\" ALIGN=left HEIGHT=115 WIDTH=150>","http:\/\/www.eyehike.com\/pgallery\/i.php?\/galleries\/Ape_Canyon_Photos_WA\/PICT0114-th.jpg","","","",""]]}
这是我的代码:
<!DOCTYPE html>
<head>
<?php
require("phpsqlsearch_dbinfo.php"); // This is the file with your logon info
//$host="localhost"; //replace with your hostname
//$username="root"; //replace with your username
//$password=""; //replace with your password
//$db_name="$database"; //replace with your database
$con=mysqli_connect("$hostname", "$username", "$password", "$database")or die("cannot connect");
//mysqli_select_db("$db_name")or die("cannot select DB");
$sql = "select * from location_markers WHERE mrk_id < 3"; //replace emp_info with your table name
$result = mysqli_query($con, $sql);
$json_string_data = array();
if(mysqli_num_rows($result)){
while($row=mysqli_fetch_row($result)){
$json_string_data['cm_mapTABLE'][]=$row;
}
}
mysqli_close($con);
// You have to give the json a name to make it accessible by JS, e.g.:
// echo 'file='.json_encode($json_string_data),';';
// This statement will echo the json output in the displayed web page
echo json_encode($json_string_data);
// please refer to our PHP JSON encode function tutorial for learning json_encode function in detail
?>
</head>
<body>
<script>
for (i = 0;i < $json_string_data.length;i++){
var javascript_string_data=<?php echo json_encode($json_string_data); ?>;
document.write(cm_mapTABLE.rank[i]);
}
</script>
</body>
</html>
我正在使用 Firebug,这是错误:
ReferenceError: $json_string_data 未定义
for (i = 0;i
有人可以告诉我在我的 JSON 字符串中引用元素的正确方法吗?我是否需要以某种方式获取 JSON 字符串中的字段名称?
【问题讨论】:
标签: javascript php mysql json google-maps