我收到"firstname": "John2",的无效标签错误。我将我的 json 数据放在括号中,那为什么我仍然收到此错误?我是否仍然缺少一些东西,以至于代码将它与 java 脚本标签混淆了?
<script type="text/javascript">
var data = "(" + {
"firstname": "John1",
"lastname": "Doe",
"location": {
"latitude": "48.4048403957642",
"longitude": "2.68452644348145"
}
}
{
"firstname": "John2",
"lastname": "Doe",
"location": {
"latitude": "48.4050236871384",
"longitude": "2.68512690051361"
}
}
{
"firstname": "John3",
"lastname": "Doe",
"location": {
"latitude": "48.4048403957642",
"longitude": "2.68452644348145"
}
}
+ ")" ;
function initialize() {
var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
for (var i = 0; i < data.length; i++) {
var location = data[i];
var latLng = new google.maps.LatLng(location.latitude,
location.longitude);
var marker = new google.maps.Marker({
position: latLng
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>