尝试之前json_decode()
$input = '[\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png\",\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg\"]';
$json = json_decode(stripslashes($input),true);
剥离斜线var_dump($json)
输出给了我下面的
array(2) {
[0]=> string(64) "http://localhost/theme/wp-content/uploads/2017/08/LOGO-SEG-2.png"
[1]=> string(64) "http://localhost/theme/wp-content/uploads/2017/08/algoritims.jpg"
}
试试吧here!
编辑补充:
原始字符串是一个真正的JSON数组的字符串表示,而不是一个对象,因为它没有密钥。我在每个元素上使用url
键的对象上尝试了同样的修复,这就是结果。
$input = '[{\"url\" : \"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png\"},{\"url\" : \"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg\"}]';
$json = json_decode(stripslashes($input),true);
输出:
array(2) {
[0]=> array(1) {
["url"]=> string(64) "http://localhost/theme/wp-content/uploads/2017/08/LOGO-SEG-2.png"
}
[1]=> array(1) {
["url"]=> string(64) "http://localhost/theme/wp-content/uploads/2017/08/algoritims.jpg"
}
}
此外,\"
是不在字符串文本中一个有效的JSON字符。只有在变量声明为"
(例如:$encoded = "{\"some value\"}";
)时才有效。
echo var_dump(json_decode('{\"myKey\": \"myValue\"}', true)); // output: NULL
试试吧live。
这是治疗症状,而不是原因。另外,当任何元素包含双引号时它会中断。 – ishegg
@ishegg我不同意:“json_decode()”不起作用的原因是由于反斜杠(证明:你的答案中的字符串与OP的反斜线存在唯一的区别),我们也不知道在哪里OP从这些数据中获得,因为我们知道他/他可能无法控制输入值 –
这绝对是因为反斜杠。但问题在于'\“'是JSON对象中的一个有效字符串,所以,如果你将它们去掉,你将*在将来遇到问题(即URL带有文本描述,这可能是完美的引用它)。看看[这里](https://3v4l.org/1gqSB)。完全有效的字符串,但代码因'stripslashes()'为JSON编码的字符串而中断 – ishegg