在字符串中转义JSON控制字符 [英] Escaping of JSON control characters within string

查看:211
本文介绍了在字符串中转义JSON控制字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中编写JSON解析器时,遇到了一个化妆问题:



在JSON规范中,很明显,Javascript控制字符与例如在C和Java中,像\\\
或\t。我遇到的问题是,当JSON字符串中有控制代码(因此在引号:property:value)中时,显示的JSON代码被弄乱,因为控制字符正在更改打印,例如\\\
创建一个新行或\t创建一个选项卡。



一个例子:



String s ={\\\
\t\property1\:\快速的棕色狐狸\\ over懒狗,\\\\\ property2\:\value2\\\\
}



打印为:



{
property1:快速棕色狐狸
跳过懒狗,
property2:value2
}



解决方案如下所示:



String s ={\\\
\t\property1\:\快速的棕色狐狸\\ \\ over懒狗 \\\
\t\property2\:\value2 \\\\
}



正确地打印为



{
property1:快速棕色狐狸懒狗,
property2:value2
}



所以我的问题是:它是corr在字符串中处理控制代码外的字符串不同于控制代码?在任何控制字符之前,在JSON字符串中添加另一个反斜杠\是否正确,是否创建不会对JSON字符串的外观产生影响的\\\
或\t这样的字符串?

解决方案


将字符串外的控制代码视为不同于
是否正确?字符串内的控制代码? / p>

JSON规范状态


JSON文本是一系列令牌。一组令牌包括六个
结构字符,字符串,数字和三个文字名称。


这些是$ $ c> {, [} ] 。然后说明


在六个结构字符中的任何一个之前或之后,允许无效的空格。




您的 \\\
\t 等(实际上规则定义了其中的4个)被认为是空白的,所以你可以在上面的字符中放置尽可能多的。



没有控制字符的概念外部JSON字符串。这些只是空格字符。是的,他们被不同的对待。


在JSON字符串中添加另一个反斜杠 \
在任何控制字符之前,创建如\\\
\t
对JSON字符串的外观不会有任何影响?


在你的例子中,你正在写字符串文字。如果您真的想在JSON字符串中写入 \\\
,则需要将 \\\\
写入Java String 文字,对于其他转义序列也是类似的。 JSON生成器必须在Java String 中找到任何空格,它将转换为JSON字符串并相应地转义。 JSON解析器必须找到解析的JSON字符串中的文字 \\\
(或其他任何内容),并在Java String 它创建。


While writing a JSON parser in Java I ran into a "cosmetic" problem:

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.

An example:

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" }

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}"

Printing "correctly" as:

{ "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?

解决方案

Is it correct to treat control code outside strings differently than the control code within strings?

The JSON specification states

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

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.

There is no notion of control characters outside JSON strings. These are just whitespace characters. Yes, they are treated differently.

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?

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.

这篇关于在字符串中转义JSON控制字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆