Html JSON 转义空格字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6065679/
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-29 08:40:52  来源:igfitidea点击:

JSON escape space characters

htmljsonescapingcharacter

提问by CQM

How would I escape space characters in a JSON string? Basically my problem is that I've gotten into a situation where the program that reads the string can use HTML tags for formatting, but I need to be able to use these HTML tags without adding more spaces to the string. so things like

我将如何转义 JSON 字符串中的空格字符?基本上我的问题是我遇到了这样一种情况,即读取字符串的程序可以使用 HTML 标记进行格式化,但我需要能够使用这些 HTML 标记而无需向字符串添加更多空格。所以像

<u>text</u>

is fine, for adding underline formatting

很好,用于添加下划线格式

but something like

但像

<font size="14">text</font>

is not fine, because the <font>tag with the size attribute adds an extra space to the string. I know, funny criteria, but at this point thats what has happened.

不好,因为<font>带有 size 属性的标签给字符串增加了一个额外的空间。我知道,有趣的标准,但在这一点上,这就是发生的事情。

My first speculative solution would be to have some kind of \escape character that JSON can put in between font and size that will solve my "space" problems, something that the HTML will ignore but leave the human readable string in the code without actual spaces.

我的第一个推测性解决方案是在字体和大小之间添加某种 \escape 字符,JSON 可以将其放在字体和大小之间,以解决我的“空格”问题,HTML 会忽略这些问题,但在代码中留下人类可读的字符串,而没有实际空格.

 ex. <font\&size="14">text</font>

displays as: text

显示为: text

kind of like &nbsp;but better?

有点像&nbsp;但更好?

any solutions?

任何解决方案?

回答by carlosfigueira

You can use \u0020 to escape the ' ' character in JSON.

您可以使用 \u0020 来转义 JSON 中的 ' ' 字符。