<?php
$json = file_get_contents('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=XXXXX&api_key=XXXX&format=json');
$track_data = json_decode($json);
foreach ($track_data as $data)
{
if (!empty($data)) {
$text = $data->recenttracks->track->artist->mbid;
}
echo '<span>'; echo $text; echo '</span>';
}
?>
这是我要使用的代码。问题是:什么都没有出现……有什么问题吗?:)这是JSON输入(file_get_contents上的URL内容):http://pastebin.com/NLHGMapT
Track是一个数组:
$text = $data->recenttracks->track[0]->artist->mbid;
同样,当你在foreach循环中你不需要使用recenttracks:
$text = $data->track[0]->artist->mbid;