简体   繁体   English

json_decode在php进程中有一些特殊的字符

[英]json_decode in php process some special character

Here is the php code: 这是php代码:

$str='{"key":"'.chr(1).'"}';
$json=json_decode($str);

json_decode return null. json_decode返回null。 So how should I process the $str in order to decode. 那么我应该如何处理$ str才能解码。 (PS $str here is just a example, it may include chr(2), chr(10) and so on). (PS $ str只是一个例子,它可能包括chr(2),chr(10)等等)。

Only characters in the range U+0020-U+0021, U+0023-U+005B, U+005D-U+10FFFF may appear unescaped in strings. 只有U + 0020-U + 0021,U + 0023-U + 005B,U + 005D-U + 10FFFF范围内的字符可能不会出现在字符串中。 Any other character must be escaped using the Unicode escape sequence. 必须使用Unicode转义序列转义任何其他字符。 In your case use \ instead. 在你的情况下使用\代替。

As Gumbo points out, you are not generating valid JSON. 正如Gumbo所指出的那样,你没有生成有效的JSON。 If you must do this (unsure why, your example is basic a no-op), then use json_encode() : 如果你必须这样做(不确定为什么,你的例子是基本的无操作),那么使用json_encode()

$str = '{"key":'.json_encode(chr(1).chr(2)).'}';
$json = json_decode($str);

This creates valid JSON: 这会创建有效的JSON:

{"key":"\u0001\u0002"}

And will decode correctly. 并将正确解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM