无法解析JSON文件中的TAB [英] Unable to parse TAB in JSON files
问题描述
在加载似乎包含 TAB 字符的JSON文件时,我遇到了解析问题.
I am running into a parsing problem when loading JSON files that seem to have the TAB character in them.
当我转到 http://jsonlint.com/时,我输入了带有TAB字符的部分:
When I go to http://jsonlint.com/, and I enter the part with the TAB character:
{
"My_String": "Foo bar. Bar foo."
}
验证者抱怨:
Parse error on line 2:
{ "My_String": "Foo bar. Bar foo."
------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
这实际上是有问题的JSON文本的复制/粘贴.
This is literally a copy/paste of the offending JSON text.
我尝试用json
和simplejson
加载此文件,但未成功.如何正确加载?我应该只预处理文件并用\t
或空格代替TAB吗?还是我在这里缺少什么?
I have tried loading this file with json
and simplejson
without success. How can I load this properly? Should I just pre-process the file and replace TAB by \t
or by a space? Or is there anything that I am missing here?
这也是simplejson
中的一个有问题的示例:
Here is also a problematic example in simplejson
:
foo = '{"My_string": "Foo bar.\t Bar foo."}'
simplejson.loads(foo)
JSONDecodeError: Invalid control character '\t' at: line 1 column 24 (char 23)
推荐答案
来自 JSON标准:
在任何令牌之前或之后都可以使用无关紧要的空格.这 空格字符为:字符列表(U + 0009),换行符 (U + 000A),回车(U + 000D)和空格(U + 0020).空格为 不允许在任何令牌中使用,除非在其中允许使用空格 字符串.
Insignificant whitespace is allowed before or after any token. The whitespace characters are: character tabulation (U+0009), line feed (U+000A), carriage return (U+000D), and space (U+0020). Whitespace is not allowed within any token, except that space is allowed in strings.
这意味着JSON字符串中不允许使用文字制表符.您需要将其转义为\t
(在.json文件中):
It means that a literal tab character is not allowed inside a JSON string. You need to escape it as \t
(in a .json-file):
{"My_string": "Foo bar.\t Bar foo."}
此外,如果在Python字符串文字中提供json文本,则您需要对标签进行两次转义:
In addition if json text is provided inside a Python string literal then you need double escape the tab:
foo = '{"My_string": "Foo bar.\\t Bar foo."}' # in a Python source
或使用Python原始字符串文字:
Or use a Python raw string literal:
foo = r'{"My_string": "Foo bar.\t Bar foo."}' # in a Python source
这篇关于无法解析JSON文件中的TAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!