18

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)

4
  • Did you tried using Map? Commented Aug 8, 2015 at 12:11
  • how can i use map for this .? i have no idea Commented Aug 8, 2015 at 12:14
  • 1
    That is not JSON. Strings need to be in double quotes, not single quotes. Commented Aug 8, 2015 at 12:16
  • Valid json string is: {"content":{"path":"callie/circle","type":"file"},"video":{"videoId":"CvIr-2lMLsk","endSeconds":"30'","startSeconds":15}} I suggest you to create correct json string first. Commented Aug 8, 2015 at 12:19

1 Answer 1

40

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);
Sign up to request clarification or add additional context in comments.

2 Comments

how can i use incorrect json string,just pure js dict ? {}
like {url:xxxx}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.