Parsing string as JSON with single quotes?

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

    var str =  "{'a':1}";
    str = str.replace(/'/g, '"')
    obj = JSON.parse(str);
    console.log(obj);
    

    This solved the problem for me.

提交回复
热议问题