Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Can';无法解析JSON,因为JSON字符串中有尾随空格_Node.js_Json_Parsing - Fatal编程技术网

Node.js Can';无法解析JSON,因为JSON字符串中有尾随空格

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

我目前正在使用web scraper,我从
获取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                                             ","...'