Parsing string as JSON with single quotes?

后端 未结 7 906
走了就别回头了
走了就别回头了 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:31

    Using single quotes for keys are not allowed in JSON. You need to use double quotes.

    For your use-case perhaps this would be the easiest solution:

    str = '{"a":1}';
    

    Source:

    If a property requires quotes, double quotes must be used. All property names must be surrounded by double quotes.

提交回复
热议问题