简体   繁体   English

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

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

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 (ie description ): ...但它也删除了值中的空格(即description ):

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

I need to obtain 我需要获得

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

Tx. TX。

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.stringify with 1 parameter is guaranteed to give the shortest possible JSON, meaning no obsolete whitespace outside keys and values. 是的,带有1个参数的JSON.stringify可以保证提供最短的JSON,这意味着键和值之外没有过时的空格。 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都是一种高效的递归数据序列化方法 - 默认情况下不需要使用不必要的空格。

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. space参数可用于控制最终字符串中的间距。 If it is a number, successive levels in the stringification will each be indented by this many space characters (up to 10). 如果是数字,则字符串化中的连续级别将由这么多空格字符(最多10个)缩进。 If it is a string, successive levels will indented by this string (or the first ten characters of it). 如果它是一个字符串,则连续的级别将由此字符串(或其前十个字符)缩进。

As a final note, you shouldn't even want to use a regexp for this. 最后一点,你甚至不应该使用正则表达式这一点。 Regexps are for making sense out of character patterns, not for processing structured data like XML, HTML or JSON. Regexp用于理解字符模式,而不是用于处理XML,HTML或JSON等结构化数据。 In those cases, use an XML, DOM or JSON parser respectively and process the results in there. 在这些情况下,分别使用XML,DOM或JSON解析器并在那里处理结果。

Parse the JSON, then stringify it again. 解析JSON,然后再次对其进行字符串化。 Parsing it will convert the JSON to a Javascript object, and then stringifying it will return the shortest JSON representation: 解析它会将JSON转换为Javascript对象,然后对其进行字符串化将返回最短的JSON表示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM