I'm using API to pull soccer data from betsapi. As I'm not expert in JSON, I'm sharing my queries here. My json output looks like this:
{
"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"
}
}
]
}
]
},
And my code to print result is
$obj = [''] // code above
$obj = json_decode($data);
foreach($obj as $result){
echo $result->season->name; // or
echo $result->overall->tournaments->name; // or
}
Though I've tried it with many alternatives, it doesn't print anything neither does it gives any error.
If anyone can point out the errors, I'd very much appreciate it.
var_dump()json decoded value.json_decode()will return null if data can't be decoded.