我有一个像这样的字符串,从数据库中重新获得。我需要将字符串转换为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 :(得分:12)
请更正您的JSON字符串,
Valid json is: {"content":{"path":"callie/circle","type":"file"},"video":{"videoId":"CvIr-2lMLsk","endSeconds":"30","startSeconds":15}}
然后将字符串转换为JSON:
var obj = JSON.parse(string);
我尝试了什么:
var obj = JSON.parse('{"content":{"path":"callie/circle","type":"file"},"video":{"videoId":"CvIr-2lMLsk","endSeconds":"30","startSeconds":15}}');
console.log(obj.content);
var obj2 = obj.content;
console.log(obj2.path);>>callie/circle