Node.js Can';无法解析JSON,因为JSON字符串中有尾随空格
我目前正在使用web scraper,我从Node.js Can';无法解析JSON,因为JSON字符串中有尾随空格,node.js,json,parsing,Node.js,Json,Parsing,我目前正在使用web scraper,我从获取JSON格式正确的JSON字符串不能包含任何文字换行符-它只能包含换行符的表示形式,例如\r\n。将所有文字换行符替换为\n,您应该能够正确解析它: constjsonstr=`{“description”:“\r\n “,“@type”:“组织”}`; const jsonStrWithoutNewlines=jsonStr.replace(/[\n\r]+/g,'\\n'); const obj=JSON.parse(jsonStrWithou
获取JSON格式正确的JSON字符串不能包含任何文字换行符-它只能包含换行符的表示形式,例如\r\n
。将所有文字换行符替换为\n
,您应该能够正确解析它:
constjsonstr=`{“description”:“\r\n
“,“@type”:“组织”}`;
const jsonStrWithoutNewlines=jsonStr.replace(/[\n\r]+/g,'\\n');
const obj=JSON.parse(jsonStrWithoutNewlines);
控制台日志(obj)代码>没有字符串操作?
嗯?您必须以某种方式操纵字符串,以便对其进行更改以使其可解析,这是什么意思?又一个例子,JSON如何被过度用于“可编辑的”配置文件。。
{"...821", "description":" \r\n
","@type":"Organization",...}
ErrorEXError [JSONError]: Unexpected token in JSON at position 1432 while parsing near '...821","description":" \r\n ","...'