gpt4 book ai didi

PHP json_decode 一个 json_encoded 字符串

转载 作者:行者123 更新时间:2023-12-04 15:57:13 25 4
gpt4 key购买 nike

我不太明白以下内容...

// array to encode
$a = ['regex' => '\/\+\/'];

// decoding an encoded array works
print_r(json_decode(json_encode($a), true));

// encoded array
echo json_encode($a);

// trying to decode the before encoded string doesn't work
print_r(json_decode('{"regex":"\\\/\\+\\\/"}', true));
echo json_last_error_msg();

最后一条错误消息显示语法错误。难道我不应该能够轻松解码一个简单的编码字符串吗?

我知道问题出在反斜杠上,但我不想进行任何魔术字符串替换或正则表达式来使解码工作。只是想了解哪里出了问题以及这种情况的最佳实践是什么?

我使用的是 PHP 版本 5.5.9

最佳答案

由于这个答案找到了一些东西:https://stackoverflow.com/a/10929508/1685538

如果您不使用 echo 输出的字符串,而是使用 var_export(json_encode($a)); ( documentation ),它会为您提供 {“正则表达式”:“\\\\\\/\\\\+\\\\\\/”}

print_r(json_decode('{"regex":"\\\\\\/\\\\+\\\\\\/"}', true)); 给出预期结果:

Array
(
[regex] => \/\+\/
)

没有错误。

关于PHP json_decode 一个 json_encoded 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32568211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com