且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试使用JavaScript将字符串转换为JSON时,出现SyntaxError:JSON解析错误:预期为'}'

更新时间:2022-03-16 22:29:07

如果无法更改此python字典,则可以使用解决方法:

If you cannot change this python dictionary, you can use a workaround:

var str = "[{u'total': '54', u'value': '54', u'label': u'14 Sep'}, {u'total': '58', u'value': '4', u'label': u'15 Sep'}, {u'total': '65', u'value': '7', u'label': u'16 Sep'}]";

str = str.replace(/u?'(.+?)': u?'(.+?)'/g, '"$1":"$2"');

console.log(str);

console.log(JSON.parse(str));