코드가 있습니다.
$(document).ready(function() {
var adress = [];
var locations = ' [';
$('.office p').each(function(el) {
adress[el] = $(this).text();
locations = locations + '{address:\''+adress[el]+'\', data: \'0\', options:{icon: "http://selectner.com/img/bullet.png"}},';
});
locations = locations + ']';
console.log(locations);
$('#tablink').click(function (e) {
$('#map').gmap3({
map:{
options:{
center:[51.4675954,0.048876],
zoom: 2,
scrollwheel: true,
draggable: true,
}
},
marker:{
values : locations,
options:{
draggable: true
},
events:{
}
}
});
});
});
console.log 결과를 복사하여 JSON (gmap3 함수-위치 위치)에 붙여 넣으면 모두 작동하지만 지금은 작동하지 않습니다.
JSON.parse ()를 사용해야한다고 생각하지만 오류를 씁니다.
SyntaxError : JSON.parse : 예상 속성 이름 또는 JSON 데이터의 1 행 3 열에 '}'
이 문제를 어떻게 해결할 수 있습니까?
문자열 연결 작업을 사용하여 JSON을 빌드하지 마십시오. 그냥하지 마십시오 . 절대로 .
JSON serializer
유효하지 않은 JSON을 얻지 않도록 이러한 목적으로 설계된 단순 을 사용해 볼 수 있습니다 .
var addresses = [];
$('.office p').each(function() {
addresses.push({
address: $(this).text(),
data: 0,
options: {
icon: 'http://selectner.com/img/bullet.png'
}
});
});
var locations = JSON.stringify(addresses);
console.log(locations);
// At this stage it is guaranteed that the addresses variable will
// contain valid JSON string
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다