2011-05-30 40 views
1

我正在使用StreamOn,一个流式音频提供程序来为在线广播电台分发我的音频流。他们使当前“正在播放”的歌曲信息在其服务器上可用(它们向我提供包含此信息的URL)。这是当我去到URL所显示的数据:解析json文本,然后将其显示在网页上

{ 
    "interval": { 
    "id": 0, 
    "starts_at": 1306738563270, 
"ends_at": 1306738735270 
    }, 
    "identifier": "Music-FS680", 
    "category": "music", 
    "original_category": "Music", 
    "buy_link": "", 
    "title": "I'm That Kind of Girl", 
    "artist": "Patty Loveless", 
    "album": "On Down the Line", 
    "publisher": "UMG Recordings, Inc.", 
    "itunes_song_id": "1290089", 
    "album_art": { 
    "src": "http://www.streamon.fm/player/getAlbumArt.php?u=http://a6.mzstatic.com/us/r1000/016/Features/20/41/7b/dj.twfxwryv.170x170-75.jpg", 
    "width": 170, 
    "height": 170, 
    "alt": "On Down the Line", 
    "link": "" 
    }, 
    "next_song": "Little Bitty by Alan Jackson", 
    "next_buy_link": "", 
    "next_album_art": { 
    "src": "http://www.streamon.fm/player/getAlbumArt.php?u=http://a5.mzstatic.com/us/r1000/025/Features/b5/cb/0b/dj.frlbluta.170x170-75.jpg", 
    "width": 170, 
    "height": 170, 
    "alt": "Everything I Love", 
    "link": "" 
    }, 
    "banner": { 
    "src": "", 
    "width": 0, 
    "height": 0, 
    "alt": "", 
    "link": "" 
    } 
} 

我需要采取动态数据,并清晰地在我的主页显示它,使它看起来像这样:

Title: I'm That Kind of Girl 
Artist: Parry Loveless 
Album: On Down the Line 

我知道这是文本解析,但我似乎无法弄清楚我需要使用什么类型的文本解析方法。

+0

有你的编程语言的经验吗?服务器端(PHP)或客户端(JavaScript?)。如果手动解析文本(例如手动更新标题,艺术家和专辑)或自动(例如,应自动检索和处理网页) – Lekensteyn 2011-05-30 07:38:03

+0

我有一些JavaScript的使用经验。文本需要自动解析。每当新歌开始播放时,JSON数据就会更新。 – Luke 2011-05-30 07:40:41

+0

@Luke:该文本如何在您的页面上显示?你能控制输出吗?如果你有一个服务器语言,那么这是首选,因为非JavaScript用户也可以看到歌曲。 – Lekensteyn 2011-05-30 07:43:04

回答

2

这是JSON。各种语言解析器可在http://json.org/

GoDaddy支持PHP作为服务器端语言。从外部服务器解析JSON响应的一种快捷方式。用.php扩展(如currently_playing.php)保存下面的代码:

<?php 
// retrieve the contents of the URL 
$ch = curl_init('http://wtsh.streamon.fm/card'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$res = curl_exec($ch); 
curl_close($ch); 
// parses the HTTP response and checks if the title exist 
if (($json = json_decode($res)) && $json->title) { 
    echo 'Title: ' . htmlspecialchars($json->title) . '<br>'; 
    echo 'Artist: ' . htmlspecialchars($json->artist) . '<br>'; 
    echo 'Album: ' . htmlspecialchars($json->album) . '<br>'; 
} else { 
    echo 'No information available, please check again later'; 
} 
?> 

通常情况下,你会做的结果,一些缓存,更新歌曲信息每10秒应该罚款。

看起来响应包含歌曲结束时间的数据(以毫秒为单位)。那么更好的方法是检查这个时间是否已经过去,如果是,则更新缓存。

<?php // filename: current_song.php 
$json = null; 
$cache = 'song.json'; 
// if a cache exists and the time has not passed, use it 
if (file_exists($cache)) { 
    $json = json_decode(file_get_contents($cache)); 
    if ($json->interval && $json->interval->ends_at/1000 < time()) { 
     // expired, discard json 
     $json = null; 
    } 
} 
// if there is no usuable cache 
if (!$json) { 
    // retrieve the contents of the URL 
    $ch = curl_init('http://wtsh.streamon.fm/card'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $res = curl_exec($ch); 
    curl_close($ch); 
    $json = json_decode($res); 
    // if the title exists, assume the result to be valid 
    if ($json && $json->title) { 
     // cache it 
     $fp = fopen('song.json', 'w'); 
     fwrite($fp, $res); 
     fclose($fp); 
    } else { 
     $json = null; 
    } 
} 
if ($json) { 
    $info = array(); 
    // contains the time in milliseconds 
    $info['wait_ms'] = $json->interval->ends_at - 1000 * microtime(true); 
    $info['title'] = $json->title ; 
    $info['artist'] = $json->artist; 
    $info['album'] = $json->album ; 
    $info['image'] = $json->album_art; 
    // display a JSON response for the HTML page 
    echo json_encode($info); 
} 
?> 

将它嵌入在HTML页面,使用:

<img id="song_image"><br> 
Title: <span id="song_title">-</span><br> 
Artist: <span id="song_artist">-</span><br> 
Album: <span id="song_album">-</span> 
<script> 
(function() { 
    // we need a JSON parser, if it does not exist, load it 
    if (typeof JSON == "undefined") { 
     var s = document.createElement("script"); 
     // json2.js retrieved from https://github.com/douglascrockford/JSON-js 
     s.src = "json2.js"; 
     document.getElementsByTagName("head").appendChild(s); 
    } 
})(); 
var song_ends = 0; 
function update_song() { 
    if ((new Date).getTime() < song_ends) { 
     // use cached result as the song has not ended yet 
     return; 
    } 
    var req = new XMLHttpRequest(); 
    // IE compatbility: 
    var textContent = 'textContent' in document ? 'textContent' : 'innerText'; 
    req.onreadystatechange = function() { 
     if (req.readyState == 4) { 
      var song = JSON.parse(req.responseText); 
      if (song.title) { 
       var img = document.getElementById("song_image"); 
       img.alt = song.image.alt; 
       img.src = song.image.src; 
       img.width = song.image.width; 
       img.height = song.image.height; 
       document.getElementById("song_title")[textContent] = song.title ; 
       document.getElementById("song_artist")[textContent] = song.artist; 
       document.getElementById("song_album")[textContent] = song.album ; 
       // store the end date in javascript date format 
       song_ends = (new Date).getTime() + song.wait_ms; 
      } 
     } 
    }; 
    req.open('get', 'current_song.php', true); 
    req.send(null); 
} 
// poll for changes every second 
setInterval(update_song, 1000); 
// and update the song information 
update_song(); 
</script> 
+0

这很好!我如何设置自动10秒更新? – Luke 2011-05-30 08:15:53

+0

想出了如何将它嵌入到html页面......这是一个愚蠢的问题。感谢有关更新的信息!还有一个问题,我会离开你一个人。 JSON数据包含专辑封面图片的网址。我如何在代码中获得这些信息,以便专辑封面显示在文本信息旁边? – Luke 2011-05-30 08:32:56

+0

@Luke:我用图像示例更新了第二个。 – Lekensteyn 2011-05-30 08:59:08

2

这是JSON解析,而不是文本解析。根据您用来解码JSON的语言,您可以使用一些现成功能以非常简单直接的方式获取所需的值。

在PHP中,你可以使用json_decode功能

在JavaScript中,JSON是本地的,看here如何访问您的数据成员

+0

你能推荐一个好的JSON解析器吗?我只需要在html页面上显示解析的数据。 – Luke 2011-05-30 07:39:29

+0

用什么语言? – 2011-05-30 07:40:54

+0

也许可以用JavaScript? – Luke 2011-05-30 07:41:27

相关问题