简体   繁体   中英

Decoding JSON input in PHP

<?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>';
    }
?>

This is the code I'm trying to use. The problem is: nothing shows up... What's wrong with it? :) Here's the JSON input (the content of the URL on file_get_contents): http://pastebin.com/NLHGMapT

Track is an array:

$text = $data->recenttracks->track[0]->artist->mbid;

also, when you're in the foreach loop you don't need to use recenttracks:

$text = $data->track[0]->artist->mbid;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM