[英]Nodejs - Convert a '{a:1}'-style string (generated by `util.format()`) into an objects
util.format()
generates '{a:1}'
-style strings which are not valid JSON. util.format()
生成'{a:1}'
-风格的字符串,这些字符串不是有效的JSON。 (Note that keys are not surrounded by double quotes.) How to convert this kind of strings back into objects? (请注意,键没有用双引号引起来。)如何将这种字符串转换回对象?
如果绝对值得信赖,请使用eval
:
eval('(' + str + ')');
除非您有很好的理由,否则应该使用JSON.stringify()
将对象转换为字符串,并使用JSON.parse()
将对象从字符串重新构成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.