【发布时间】:2015-11-19 01:02:55
【问题描述】:
嘿,我想把这个字符串转换成 JSON:
var newPart = '{\'' + name + '\': {\'content-type\' : ' + type + ', \'content\': ' + content + ',\'type\': \'content\'}}';
结果给出以下字符串:
{'partHtml': {'content-type' : text/html, 'content': dfg,'type': 'content'}}
我想从此字符串中获取 JSON 对象,我尝试了以下操作,但失败并出现错误:"SyntaxError: Unexpected token '"
newPart = JSON.parse(newPart);
我尝试不使用 ' 进行一些更改,但问题仍然存在。
感谢您的帮助。
【问题讨论】:
-
将所有单引号改为双引号。
-
所有字符串必须用双引号引起来 { "partHtml": { "content-type": "text/html", "content": "dfg", "type": "content" } }
-
您的字符串不代表 JSON。请参阅 json.org 的 JSON 规范
标签: javascript arrays json parsing stringify