如何在php中正确打印JSON输出数据?
我正在使用API从API中提取足球数据。由于我不是JSON方面的专家,我在这里分享我的查询。我的json输出如下所示:如何在php中正确打印JSON输出数据?,php,json,Php,Json,我正在使用API从API中提取足球数据。由于我不是JSON方面的专家,我在这里分享我的查询。我的json输出如下所示: { "success":1, "results":{ "season":{ "start_time":"1502402400", "end_time":"1526335199", "has_topgoals":"1", "has_leaguetable":"1", "has_lineups":"1",
{
"success":1,
"results":{
"season":{
"start_time":"1502402400",
"end_time":"1526335199",
"has_topgoals":"1",
"has_leaguetable":"1",
"has_lineups":"1",
"name_1":"Premier League 17\/18",
"name":"Premier League 17\/18"
},
"overall":{
"tournaments":[
{
"name":"Premier League",
"rows":[
{
"pos":"1",
"sort_pos":"1",
"change":"0",
"win":"8",
"draw":"1",
"loss":"0",
"goalsfor":"32",
"goalsagainst":"4",
"points":"25",
"pct":null,
"team":{
"id":"708",
"name":"Man City",
"image_id":"17",
"cc":"gb"
}
}
{
"pos":"20",
"sort_pos":"20",
"change":"0",
"win":"1",
"draw":"0",
"loss":"8",
"goalsfor":"2",
"goalsagainst":"19",
"points":"3",
"pct":null,
"team":{
"id":"17189",
"name":"Crystal Palace",
"image_id":"7",
"cc":"gb"
}
}
]
}
]
},
我打印结果的代码是
$obj = [''] // code above
$obj = json_decode($data);
foreach($obj as $result){
echo $result->season->name; // or
echo $result->overall->tournaments->name; // or
}
虽然我尝试了很多替代方案,但它不会打印任何内容,也不会给出任何错误
如果有人能指出错误,我将不胜感激。这个怎么样:
$obj = json_decode($data, true);
print_r($obj); //Should give you your json converted to php array
然后,循环遍历结果
foreach ($obj['results'] as $result) {
echo $result['season']['name'];
}
此外,还可以循环浏览锦标赛:
foreach ($obj['results']['overall']['tournaments'] as $tournament) {
echo $tournament['name'];
}
要在每个锦标赛中循环各队,请尝试以下方法:
foreach ($obj['results']['overall']['tournaments'] as $tournament) {
echo 'Tournament: ' . $tournament['name'] . '<br />';
foreach ($tournament['rows'] as $row) {
echo 'Team: #' . $row['team']['id'] . ' ' . $row['team']['name'] . ' (' . $row['team']['image_id'] . ')' . '<br />';
}
}
foreach($obj['results']['totall']['tournaments']作为$tourbank){
回显“锦标赛:”.$Tournament['name']。
;
foreach($锦标赛['rows']作为$row){
回显“团队:#”。$row['Team']['id'].'。$row['Team']['name'.'。(“$row['Team']['image_id'.])。”
;
}
}
var\u dump()
json解码值<如果数据无法解码,code>json\u decode()将返回null。@ArtOsi json数据输出已经存在。它只是在循环中缺少了一些东西..锦标赛循环正在工作,尽管结果仍然显示为空白。。在锦标赛的子行中循环的任何提示。感谢您的帮助。您想完成什么?在锦标赛“行”下,有一个子行带有“团队”。无法循环获取信息“image\u id”。感谢您花时间回复。我将标记答案