我正在使用API从betsapi中提取足球数据。由于我不是JSON的专家,我在这里分享我的疑问。我的json输出看起来像这样:
{
"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
}
虽然我尝试了很多替代方案,但它不会打印任何内容,也不会产生任何错误。
如果有人能指出错误,我会非常感激。
答案 0 :(得分:2)
这个怎么样:
DispatchQueue.main.async {
self.performSegue(withIdentifier: "bookingSuccess", sender: nil)
}
然后,循环结果
$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'];
}