Mubashir Kp
Mubashir Kp

Reputation: 265

Converting string to javascript dictionary

I have a string like this which is retrieved from a database. I need to convert the string to a Javascript dictionary.

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

How do I convert the above string to a Javascript dictionary? Should I convert the string to json first or not? When I try json.parse, an error is shown:

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

Upvotes: 18

Views: 58286

Answers (1)

MrYo
MrYo

Reputation: 1817

Please correct your JSON string,

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

Then convert string to JSON as:

var obj = JSON.parse(string);

What I tried:

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);

Upvotes: 40

Related Questions