Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在我的网站中,我尝试将字符串转换为包含换行符的 JSON。
JSON.parse('{"hallo":"line1\r\nline2","a":[5.5,5.6,5.7]}');
这会产生“意外令牌”错误。我需要以某种方式逃避吗?
是的,您应该同时转义它们\n,\r因为它们属于控制字符列表。可以在此处找到需要转义的字符的完整列表。你的代码是
\n
\r
obj = JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');
JSFiddle:链接
尝试:
JSON.parse('{"hallo":"line1\\r\\nline2","a":[5.5,5.6,5.7]}');