JavaScript:如何从 JSON 字符串中删除除值中的空格之外的所有空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25676165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:40:56  来源:igfitidea点击:

JavaScript: how do I remove all the white spaces from a JSON string except the ones in the values?

javascriptregexjson

提问by j3d

Given the following json...

鉴于以下json ...

var body = "{ \"name\": \"test\", \"description\": \"test json\", \"website\": \"domain.com\" }"

... how do I remove all the white spaces except the ones in the values?

...如何删除除值中的空格之外的所有空格?

I've tried the following regexp...

我试过下面的正则表达式...

var body = "{ \"name\": \"test\", \"description\": \"test json\", \"website\": \"domain.com\" }".replace(/\r?\n|\r/g, "").replace(/\s+/g, "")

... but it also removes the spaces in the values (i.e. description):

...但它也会删除值中的空格(即description):

{"name":"test","description":"testjson","website":"domain.com"}

I need to obtain

我需要获得

{"name":"test","description":"test json","website":"domain.com"}

Tx.

发送。

回答by Niels Keurentjes

var body = ' { "name": "test", "description": "test json", "website": "domain.com" } ';
JSON.stringify(JSON.parse(body))

Returns:

返回:

{"name":"test","description":"test json","website":"domain.com"}

Which is exactly what you want. And yes, JSON.stringifywith 1 parameter is guaranteed to give the shortest possible JSON, meaning no obsolete whitespace outside keys and values. That's the default because JSON is, in nearly all situations, intended to be a highly efficient recursive data serialization method - unnecessary whitespace has no use there by default.

这正是您想要的。是的,JSON.stringify使用 1 个参数可以保证提供尽可能短的 JSON,这意味着键和值之外没有过时的空格。这是默认设置,因为在几乎所有情况下,JSON 都旨在成为一种高效的递归数据序列化方法 - 默认情况下,不必要的空格在那里没有用处。

The full syntax is actually:

完整的语法实际上是

JSON.stringify(value[, replacer [, space]])

The optional third parameter defaults to false- if set to another value it produces 'pretty print' with extra whitespace or lead-in strings:

可选的第三个参数默认为false- 如果设置为另一个值,它会生成带有额外空格或导入字符串的“漂亮打印”:

The space argument may be used to control spacing in the final string. If it is a number, successive levels in the stringification will each be indented by this many space characters (up to 10). If it is a string, successive levels will indented by this string (or the first ten characters of it).

space 参数可用于控制最终字符串中的间距。如果它是一个数字,则字符串化中的连续级别将每个缩进这么多空格字符(最多 10 个)。如果它是一个字符串,后续级别将以此字符串(或它的前十个字符)缩进。

As a final note, you shouldn't even wantto use a regexp for this. Regexps are for making sense out of character patterns, not for processing structured data like XML, HTML or JSON. In those cases, use an XML, DOM or JSON parser respectively and process the results in there.

最后一点,你甚至不应该使用正则表达式这一点。正则表达式用于理解字符模式,而不是用于处理 XML、HTML 或 JSON 等结构化数据。在这些情况下,分别使用 XML、DOM 或 JSON 解析器并在其中处理结果。

回答by Barmar

Parse the JSON, then stringify it again. Parsing it will convert the JSON to a Javascript object, and then stringifying it will return the shortest JSON representation:

解析 JSON,然后再次对其进行字符串化。解析它会将 JSON 转换为 Javascript 对象,然后将其字符串化将返回最短的 JSON 表示:

var body = JSON.stringify(JSON.parse("{ \"name\": \"test\", \"description\": \"test json\", \"website\": \"domain.com\" }"));