I get invalid label error for “firstname”: “John2”,.
I put my json data in parenthesis, so why am I still getting this error?
Am I still lacking something so that the code is confusing it with a java script label?
<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>
You have two problems (that I can spot).
First, you are working in JavaScript, not JSON. You have an object literal.
Get rid of the
"("and")"entirely. Those are used if you are trying to parse JSON usingeval. You don’t have JSON and if you did you should use theJSONobject (and itsparsemethod), noteval. Currently, you are trying to stringify the object (which would give you something like"([Object object])", which wouldn’t be any use at all).Second, you appear to be trying to work with an array of objects, but you have just placed them one after the other.
Wrap your objects with an array literal
[and].Separate the items in the array with commas (
,).The reason for the error message is that you have created such a weird parse state that the JS engine things you are trying to label a loop.