json_decode()(PHP 7)中的新行和新标签

问题描述:

我的使用json_decode()的代码在PHP 5.6上可以正常工作.移植到PHP 7.0后,json_decode()返回NULL,并且json_last_error()告诉我我的错误是:

My code using json_decode() worked correctly with PHP 5.6. After migration to PHP 7.0, json_decode() returns NULL and json_last_error() tells me that my error is:

控制字符错误,可能编码错误

Control character error, possibly incorrectly encoded

调试后,我发现我的问题是字符串值中的制表符和换行符.如果我将它们都删除,它会起作用.如果我在选项卡上留下 新行,则会发生错误.

After debugging, I found out that my problem are both tabs and new line characters in string values. If I remove them both, it works. If I leave either new lines or tabs, the error occurs.

json_decode()行为在PHP 7中是否已更改?我想在我的.json文件中保留制表符和换行符,以提高可读性.如果我将制表符替换为\ t并将新行替换为\ n,则该代码有效.

Is json_decode() behavior changed in PHP 7? I would like to keep tabs and new lines in my .json files for better readability. The code works if I replace tabs to \t and new lines to \n.

如何保留新行和新标签?

How can I keep new lines and tabs?

由于软件许可问题,旧的json模块已替换为jsond模块.您可以在此处看到有关此更改和附加的请求请求的讨论.现在,有关更改或解决方法的信息不多,但是我可以看到字符串([\0x00-\0x1F])中的所有控制字符都会触发错误.对于您来说不幸的是,根据 JSON标准,这种行为似乎是正确的>:

Due to a software licensing issue, the old json module was replaced with the jsond module. You can see the discussion of this change and the attached pull request here. Now, there's not much information about the changes or about workarounds for things, but I can see that all control characters inside strings ([\0x00-\0x1F]) trigger an error. Unfortunately for you, it seems that this behavior is correct per the JSON Standard:

在任何令牌之前或之后都可以使用无关紧要的空格.空格字符是:字符列表(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\u0009.因此,您使用的JSON直接违反了该标准.理想情况下,您应该获取JSON源以返回符合标准的JSON.如果那行不通,则必须预处理JSON,并将字符串中的制表符转换为\t.

So, in other words, literal tabs are not allowed inside JSON strings at all; they must be \t or \u0009. So, the JSON you're consuming is in direct violation of the standard. Ideally, you should get your JSON source to return standards-compliant JSON. If that won't work, you'll have to pre-process the JSON and convert tabs inside strings to \t.