Parsing string as JSON with single quotes?

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

    Something like this:

    var div = document.getElementById("result");
    
    var str = "{'a':1}";
      str = str.replace(/\'/g, '"');
      var parsed = JSON.parse(str);
      console.log(parsed);
      div.innerText = parsed.a;

提交回复
热议问题