2013-11-04 33 views
-2

我有問題,在我的javascript代碼JSON對象的數組,這裏是字符串JSON未捕獲的SyntaxError

[{"id":"ID", "lat":"LAT", "lon":"LON", "zip":"ZIP", "text":"TEXT"}] 

問題出現在「文本」對象中的代碼,當字符串包含「在」工作了說「未捕獲的SyntaxError:意外的標記非法」 這裏是完整的代碼:

[{"id":"1", "lat":"43.19716728250127", "lon":"-119.53125", "zip":"40219", "text":"Testing, Hello World"},{"id":"2", "lat":"46.92025531537451", "lon":"-119.443359375", "zip":"40222", "text":"hello world"},{"id":"3", "lat":"39.16414104768742", "lon":"-82.529296875", "zip":"", "text":"Choice Roof Contractor 
<br>Based in Mansfield, OH"}] 
+1

是您使用的代碼來處理呢? 'in'在Javascript中是一個完全合法的字符串標記。 – Nightfirecat

+0

無法重現 - http://jsbin.com/ElaXalo/1/ - 當您創建縮減的測試用例時,您需要提供足夠的信息來實際重現問題。 – Quentin

+4

爲什麼不在這裏發佈您正在使用的ACTUAL字符串。你爲什麼要放一些不同的截圖? – Lix

回答

0

你所面對的問題是,JavaScript不支持多行字符串。確保你的所有字符串都在一行上。

 
[{"id":"1", "lat":"43.19716728250127", "lon":"-119.53125", "zip":"40219", "text":"Testing, Hello World"},{"id":"2", "lat":"46.92025531537451", "lon":"-119.443359375", "zip":"40222", "text":"hello world"},{"id":"3", "lat":"39.16414104768742", "lon":"-82.529296875", "zip":"", "text":"Choice Roof Contractor 
<br>Based in Mansfield, OH"}] 

這應該是這樣的:

 
[{"id":"1", "lat":"43.19716728250127", "lon":"-119.53125", "zip":"40219", "text":"Testing, Hello World"},{"id":"2", "lat":"46.92025531537451", "lon":"-119.443359375", "zip":"40222", "text":"hello world"},{"id":"3", "lat":"39.16414104768742", "lon":"-82.529296875", "zip":"", "text":"Choice Roof Contractor<br>Based in Mansfield, OH"}] 

一個簡單的例子:

// Will NOT work! 
var str = "This is a 
multiline string"; 

// Will work 
var str = "This is not a multiline string"; 

// Will work 
var str = "This is a " + 
"multiline string"; 
+0

我可以張貼一些截圖,看看它在我的真實HTML源代碼中的樣子嗎? –

+1

@DarielPratama - 截圖有什麼?你正在處理文字沒有?只需發佈您正在使用的實際文本...以您的示例jsFiddle - 我需要做的所有工作就是擺脫該換行符。 – Lix

+0

@DarielPratama爲什麼截圖?把它放在小提琴裏。然後你可以輕鬆地進行調試。 –

0

這裏是工作的JavaScript。有一個在http://jsfiddle.net/tSRPV/斷行後選擇屋頂承包商導致了問題:

var _json = [{ 
    "id": "1", 
    "lat": "43.19716728250127", 
    "lon": "-119.53125", 
    "zip": "40219", 
    "text": "Testing, Hello World" 
}, { 
    "id": "2", 
    "lat": "46.92025531537451", 
    "lon": "-119.443359375", 
    "zip": "40222", 
    "text": "hello world" 
}, { 
    "id": "3", 
    "lat": "39.16414104768742", 
    "lon": "-82.529296875", 
    "zip": "", 
    "text": "Choice Roof Contractor &lt;br&gt;Based in Mansfield, OH" 
}]; 

for (i = 0; i < _json.length; i++) { 
    console.log(_json[i].text); 
} 

小提琴here

相關問題