如何从这个json获得songtitle(第9个元素) - ajax / jquery / javascript

时间:2016-04-07 16:28:12

标签: javascript jquery shoutcast

示例json:

{"currentlisteners":0,"peaklisteners":0,"maxlisteners":5,
 "uniquelisteners":0,"averagetime":0,"servergenre":"Bollywood",
 "servergenre2":"","servergenre3":"","servergenre4":"","servergenre5":"",
 "serverurl":"http:\/\/www.website.in","servertitle":"RadioName",
 "songtitle":"Udit Narayan - Bheed Mein - www.Songspk.name","dj":"encoder","songurl":"","streamhits":21514,"streamstatus":1,"backupstatus":0,"streamlisted":1,"streamsource":"203.xxx.xxx.xxx","streampath":"\/stream","streamuptime":0,"bitrate":"64","content":"audio\/mpeg","version":"2.4.7.256 (posix(linux x64))"}

new Request.JSONP({
    url: 'http://ip:port/stats?sid=1&json=1',
    onSuccess: function(response) {
        show_response(response, $('post'));
    }
}).send();
show_response = function(obj, result) {
    $H(obj).each(function(v, k) {

        new Element('div', {
            html: v
        }).inject(result);
         
    });
    

};
<div class='wrapper'>
    <div id='post'></div>
</div>

2 个答案:

答案 0 :(得分:1)

只需使用返回的对象:

show_response = function(obj, result) {
      new Element('div', {
            html: obj.songtitle
        }).inject(result); 
};

请参阅:

http://jsfiddle.net/s7trsu9r/2/

答案 1 :(得分:0)

检查键是否等于“songtitle”

new Request.JSONP({
url: 'http://ip:port/stats?sid=1&json=1',
onSuccess: function(response) {
    show_response(response, $('post'));
}
}).send();
show_response = function(obj, result) {
$H(obj).each(function(v, k) {
    if(k == 'songtitle)'{
        new Element('div', {
            html: v
        }).inject(result);
    }

});

};