我有json字符串:$json = '{ "comment" : "I don\'t like" }';
$json_array = json_decode($json, true); // decode as array rather than object
现在单个代码之前的反斜杠在这里搞乱了:
foreach($json_array as $key => $value)
{
echo $value;
}
所以我在foreach
之前尝试了这个并解码:
$json = stripslashes($json);
但仍然出错:
错误:为foreach()提供的参数无效
答案 0 :(得分:1)
这不是合法的PHP代码:
$json = { "comment" : "I don\'t like" }
如果你想在PHP中使用JSON 字符串:
$json = '{ "comment" : "I don\'t like" }';
答案 1 :(得分:1)
您需要使用json_decode将json转换为对象或数组。以下代码输出I don't like
:
$json = '{ "comment" : "I don\'t like" }';
$data = json_decode($json, true);
foreach($data as $key => $value)
{
echo $value;
}