Parsing string as JSON with single quotes?

后端 未结 7 908
走了就别回头了
走了就别回头了 2020-11-22 10:22

I have a string

str = \"{\'a\':1}\";
JSON.parse(str);
VM514:1 Uncaught SyntaxError: Unexpected token \'(…)

How can I parse the above stri

相关标签:
7条回答
  • 2020-11-22 10:51

    The JSON standard requires double quotes and will not accept single quotes, nor will the parser.

    If you have a simple case with no escaped single quotes in your strings (which would normally be impossible, but this isn't JSON), you can simple str.replace(/'/g, '"') and you should end up with valid JSON.

    0 讨论(0)
提交回复
热议问题