Java 转义字符串中的 JSON 控制字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23112193/
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
Escaping of JSON control characters within string
提问by Marcus
While writing a JSON parser in Java I ran into a "cosmetic" problem:
在用 Java 编写 JSON 解析器时,我遇到了一个“装饰性”问题:
In the JSON specification it's clearly said that Javascript control characters are the same as e.g. in C and Java, like \n or \t. The problem I was running into, is that when there are control codes within a JSON string (so within the quotes: "property":"value"), then the displayed JSON code is messed up because the control characters are changing the print, e.g. \n creates a new line or \t creates a tab.
在 JSON 规范中,它清楚地表明 Javascript 控制字符与例如 C 和 Java 中的相同,如 \n 或 \t。我遇到的问题是,当 JSON 字符串中有控制代码时(所以在引号内:"property":"value"),那么显示的 JSON 代码就会混乱,因为控制字符正在改变打印,例如 \n 创建一个新行或 \t 创建一个制表符。
An example:
一个例子:
String s = "{\n\t\"property1\": \"The quick brown fox\njumps over the lazy dog\",\n\t\"property2\":\"value2\"\n}"
String s = "{\n\t\"property1\": \"The quick brown fox\njumps over the lazy dog\",\n\t\"property2\":\"value2\"\n}"
Printing as:
打印为:
{
"property1": "The quick brown fox
jumps over the lazy dog",
"property2": "value2"
}
{
"property1": "The quick brown fox
jumps over the lazy dog",
"property2": "value2"
}
The solution would look like this:
解决方案如下所示:
String s = "{\n\t\"property1\": \"The quick brown fox\\njumps over the lazy dog\",\n\t\"property2\": \"value2\"\n}"
String s = "{\n\t\"property1\": \"The quick brown fox\\njumps over the lazy dog\",\n\t\"property2\": \"value2\"\n}"
Printing "correctly" as:
“正确”打印为:
{
"property1": "The quick brown fox\njumps over the lazy dog",
"property2": "value2"
}
{
"property1": "The quick brown fox\njumps over the lazy dog",
"property2": "value2"
}
So my question: Is it correct to treat control code outside strings differently than the control code within strings? And is it correct to add within JSON strings another backslash \ before any control characters, creating strings like "\n" or "\t" that won't have any effect on the look of JSON strings?
所以我的问题是:将字符串外的控制代码与字符串内的控制代码区别对待是否正确?在 JSON 字符串中在任何控制字符之前添加另一个反斜杠 \ 是否正确,创建像“\n”或“\t”这样的字符串,不会对 JSON 字符串的外观产生任何影响?
采纳答案by Sotirios Delimanolis
Is it correct to treat control code outside strings differently than the control code within strings?
将字符串外的控制代码与字符串内的控制代码区别对待是否正确?
The JSON specificationstates
的JSON规范状态
A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.
JSON 文本是一个令牌序列。令牌集包括六个结构字符、字符串、数字和三个文字名称。
These are {
, [
, }
, ]
, :
, and ,
. It then states
这是{
,[
,}
,]
,:
,和,
。然后它说
Insignificant whitespace is allowed before or after any of the six structural characters.
在六个结构字符中的任何一个之前或之后都允许使用无关紧要的空格。
Your \n
, \t
and others (actually the spec defines 4 of them) are considered white space, so you can put as many of them as you want around the above characters.
您的\n
,\t
和其他(实际上规范定义了其中的 4 个)被视为空白,因此您可以在上述字符周围放置任意数量的空格。
There is no notion of control characters outside JSON strings. These are just whitespace characters. Yes, they are treated differently.
在 JSON 字符串之外没有控制字符的概念。这些只是空白字符。是的,他们被区别对待。
And is it correct to add within JSON strings another backslash
\
before any control characters, creating strings like"\n"
or"\t"
that won't have any effect on the look of JSON strings?
并且
\
在任何控制字符之前在 JSON 字符串中添加另一个反斜杠是否正确,创建类似"\n"
或"\t"
不会对 JSON 字符串外观产生任何影响的字符串?
In your example, you are writing String
literals. If you literally want to write \n
in the JSON string, you need to write \\n
in the Java String
literal and similarly for the other escape sequences. The JSON generator must find any whitespace in the Java String
it is converting to a JSON string and escape it accordingly. The JSON parser must find the literal \n
(or whatever else) in the JSON string it parses and convert it appropriately in the Java String
it creates.
在您的示例中,您正在编写String
文字。如果您真的想写入\n
JSON 字符串,则需要写入\\n
JavaString
文字,其他转义序列也类似。JSON 生成器必须在String
它转换为 JSON 字符串的 Java 中找到任何空格,并相应地对其进行转义。JSON 解析器必须\n
在它解析的 JSON 字符串中找到文字(或其他任何内容),并在String
它创建的 Java 中适当地转换它。