2

我需要这样的 JSON 对象:

{
  "name": "This is a name",
  "description": "This is    the description"
}

并删除所有回车和不在引号中的空格。所以结果应该是这样的:

{"name":"This is a name","description":"This is    the description"}

引号内的空格需要保留。

一个 jQuery 解决方案很好。

谢谢!

-马特

4

2 回答 2

1

由于您是从文本字段中获取它作为字符串,您应该能够只使用现代 JavaScript 的内置函数:

var asObj = JSON.parse(asStr);
// now you have an object for use.
// assuming you want it back as a str
var asStr2 = JSON.stringify(asObj);

那应该为你做大部分的剥离。

于 2014-10-16T15:29:00.727 回答
0

在读取 CSV 文件并将其放入 JSON 时,我遇到了与回车相同的问题:

var json = JSON.stringify({"name": "This is a name","description": "This is    the description"});
json = json.replace(/\\r/g, ''); 

由于转义回车,您必须使用\\r而不是。 \r

于 2018-06-27T11:10:41.883 回答