2015-08-08 129 views
2

我有這樣其從數據庫retirieved的字符串。我需要將字符串轉換爲JavaScript字典。轉換字符串的JavaScript字典

"['content':{'type':'file','path':'callie/circle'},'video':{'videoId':'CvIr-2lMLsk','startSeconds': 15,'endSeconds': 30'}]". 

如何將上述字符串轉換爲javascript字典?我應該將字符串轉換爲json第一或不。當我嘗試json.parse,正在顯示如下所示的錯誤爲

Uncaught SyntaxError: Unexpected token ' 
at Object.parse (native) 
at <anonymous>:2:6 
at Object.InjectedScript._evaluateOn (<anonymous>:905:140) 
at Object.InjectedScript._evaluateAndWrap (<anonymous>:838:34) 
at Object.InjectedScript.evaluate (<anonymous>:694:21) 
+0

你有沒有嘗試過使用地圖? –

+0

我該如何使用地圖。我不知道 –

+0

這不是JSON。字符串需要用*雙引號*,而不是單引號。 –

回答

5

請更正你的JSON字符串,

Valid json is: {"content":{"path":"callie/circle","type":"file"},"video":{"videoId":"CvIr-2lMLs‌​k","endSeconds":"30","startSeconds":15}} 

然後將字符串轉換爲JSON爲:

var obj = JSON.parse(string); 

什麼我想:

var obj = JSON.parse('{"content":{"path":"callie/circle","type":"file"},"video":{"videoId":"CvIr-2lMLs‌​k","endSeconds":"30","startSeconds":15}}'); 
console.log(obj.content); 
var obj2 = obj.content; 
console.log(obj2.path);>>callie/circle