Q:非法字符串偏移量JSON->;PHP
我已经在stackoverflow中查看了很多关于我的问题的问题,但我没有找到任何解决方案。我不知道我的代码哪里出了问题?我在此行收到非法字符串偏移量消息:Q:非法字符串偏移量JSON->;PHP,php,json,Php,Json,我已经在stackoverflow中查看了很多关于我的问题的问题,但我没有找到任何解决方案。我不知道我的代码哪里出了问题?我在此行收到非法字符串偏移量消息: echo $value["country"] . ", " . $value["competition"] . "<br>"; 若我使用var_dump($json)查看数据的格式,那个么我会看到它是一个数组。使用var_dump($value),我看到有一个字符串。请帮助我,我的代码中哪里有问题,为什么不能回显值国家/地区和
echo $value["country"] . ", " . $value["competition"] . "<br>";
若我使用var_dump($json)查看数据的格式,那个么我会看到它是一个数组。使用var_dump($value),我看到有一个字符串。请帮助我,我的代码中哪里有问题,为什么不能回显值国家/地区和值竞争?问题是您有两次编码的json,包括数组和数组中的元素 您应该避免这样做,但如果无法更改源,则还需要解码值:
<?php
// file_get_contents call instead
$str = file_get_contents('general.json');
$json = json_decode($str,true);
foreach($json as $key => $value){
// decode the $value string
$value = json_decode($value, true);
echo $value["country"] . ", " . $value["competition"] . "<br>";
}
?>
.为了将来,请尝试使用!我已经找了好几个小时了。谢谢。
["{\"country\":\"America\",\"competition\":\"Copa America\",\"club\":\"BOCA JUNIORS\"}","{\"country\":\"Germany\",\"competition\":\"Bundesliga\",\"club\":\"HANNOVER\"}","{\"country\":\"Asia\",\"competition\":\"JLeague\",\"club\":\"NAGOYA\"}"]
<?php
// file_get_contents call instead
$str = file_get_contents('general.json');
$json = json_decode($str,true);
foreach($json as $key => $value){
// decode the $value string
$value = json_decode($value, true);
echo $value["country"] . ", " . $value["competition"] . "<br>";
}
?>