Php Foreach URL JSON解码
Php Foreach URL JSON解码,php,json,Php,Json,$urlsDB是一个包含JSON的变量 print_r($urlsDB ); 产出: [\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png\",\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg\"] 如何正确使用json\u decode创建foreach &
$urlsDB
是一个包含JSON的变量
print_r($urlsDB );
产出:
[\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png\",\"http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg\"]
如何正确使用json\u decode
创建foreach
<?php
$urls = json_decode($urlsDB , true);
if ($urls != '' ) {
foreach ($urls as $url) {
?>
<img src="<?php echo $url;?>" class="img-responsive img-thumbnail " />
<?php
}
}
?>
“class=“img响应img缩略图”/>
Var\u dump($url);
返回空。您得到的JSON无效。您在开头和结尾都转义了引号。这是有效的:
["http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png","http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg"]
也许你在处理数据库之前对它应用了一些过滤器
这应该是有效的:
<?php
$a = '["http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/LOGO-SEG-2.png","http:\\/\\/localhost\\/theme\\/wp-content\\/uploads\\/2017\\/08\\/algoritims.jpg"]
';
$urls = json_decode($a, true);
if (count($urls) > 0) {
foreach ($urls as $url) {
?>
<img src="<?php echo $url;?>" class="img-responsive img-thumbnail " />
<?php
}
}
“class=“img响应img缩略图”/>
尝试在json\u decode()之前去掉斜杠
var\u 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"
}
试试看
编辑以添加:
原始字符串实际上是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\“}”;
)
试试看。在保存之前,我使用了htmlspecialchars
,因为我无法在输入中保存数组,所以我将数组转换为JSONY您在编码为JSON后使用了htmlspecialchars()?你应该在编码之前做,然后编码。那你就不会有任何问题了。或者,更好的方法是,在解码后,打印每个元素时才执行此操作。更好的解决方案是根本不使用htmlspecialchars。你在展示东西之前就这么做了,不是为了saving@F埃利克斯加农·格雷尼尔同意了!几分钟前我写了一些类似的东西,请查看编辑后的答案。@ishegg是的,我理解这是一个很好的方向,我同意你的看法:)非常感谢你这是在治疗症状,而不是原因。另外,当any元素包含双引号时,它将中断。@ishegg我不同意:之所以json_decode()
不起作用,是因为反斜杠(证明:答案中的字符串与OP的唯一区别是反斜杠的存在)此外,我们不知道OP从哪里获取数据,据我们所知,他/她可能无法控制输入值,这完全是因为反斜杠。但是问题是,\“
在JSON对象中是一个有效的字符串。因此,如果将它们去掉,将来会出现问题(即URL附带一个文本描述,其中可能完全有引号)。请看。完全有效的字符串,但代码会因为stripslashes()
对JSON编码字符串进行分隔而中断。
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"
}
}
echo var_dump(json_decode('{\"myKey\": \"myValue\"}', true)); // output: NULL