2013-02-27 91 views
-1

我有一個字符串,下面我試圖轉換成一個JSON對象,並使用語法var json = JSON.parse(myjson);,但它是說預期的令牌}但我看不到在哪裏?{name:「Name」,value:1}無效JSON?

有誰知道這會是哪裏?

[{name:"After", value:1},{name:"watching", value:1},{name:"you", value:1},{name:"at", value:3},{name:"Birmngm", value:1},{name:"my", value:1},{name:"new", value:2},{name:"athlete", value:1},{name:"to", value:1},{name:"follow.", value:1},{name:"Love", value:2},{name:"the", value:4},{name:"passion,", value:1},{name:"enthusiasm", value:1},{name:"and", value:3},{name:"big", value:1},{name:"smiles!!", value:1},{name:"Long", value:1},{name:"may", value:1}] 
+6

這是一個很好的工具:http://jsonlint.com/ – 2013-02-27 21:00:31

+1

不,它不是。屬性名稱缺少字符串分隔符。 – Bergi 2013-02-27 21:01:09

+0

請給我們的代碼。你誤以爲JSON的簡單對象文字? – Bergi 2013-02-27 21:02:16

回答

8

這是完全無效的JSON。每一個關鍵都必須引用。例如

[{"name":"After","value":1}, .... 
    ^-- ^--  ^-- ^-- 
+0

謝謝:)我沒有意識到 – user1840255 2013-02-27 21:01:58

+1

不要自己生成json字符串。這太痛苦/不可靠。使用您的主機語言的json接口,例如php的'json_encode()'。 – 2013-02-27 21:02:38

+0

@MarcB:但手工編寫PHP關聯數組?在某些時候,數據需要被插入到系統中,JSON(如果正確縮進)是一個非常簡單的格式。 – Bergi 2013-02-27 21:08:45

1

這是無效的json。你需要把引號鑰匙這樣

{ 
    "name": "After", 
    "value": 1 
}, 

JSONLint對於調試JSON問題的一個很好的工具,它可以告訴你一個特定的字符串是否是有效的,並指出你到一個問題是。

1

這是有效的JavaScript,無效的JSON。您可以打開鉻合金開發者控制檯,而這個字符串化得到有效的JSON:

[{"name":"After","value":1},{"name":"watching","value":1},{"name":"you","value":1},{"name":"at","value":3},{"name":"Birmngm","value":1},{"name":"my","value":1},{"name":"new","value":2},{"name":"athlete","value":1},{"name":"to","value":1},{"name":"follow.","value":1},{"name":"Love","value":2},{"name":"the","value":4},{"name":"passion,","value":1},{"name":"enthusiasm","value":1},{"name":"and","value":3},{"name":"big","value":1},{"name":"smiles!!","value":1},{"name":"Long","value":1},{"name":"may","value":1}] 
1

提供的字符串是正確的,如果你嘗試建立一個JS對象。但是,爲了創建一個有效的JSON字符串,你需要用引號括住這些鍵。因此您應該用雙引號或雙引號封裝namevalue。您可以使用jsonlint.com進行驗證。

相關問題