Home  >  Q&A  >  body text

javascript - json遍历

js有点不熟,弄了半天还弄不好!求大神们

JsonCallBack({"retcode":"0","uin":"0","total":"10","start":"-1","songs":[{"id": "0","data": "http://xxx.xxx.cn/zorro.mp3|用户|http://xxx.xxx.cn/zorro.gif|类别|http://xxx.xxx.cn/zorro.gif|标题http://xxx.xxx.cn/zorro.gif|271|1|0|0|10791177|0|320000|0|29298365|30294145|6185642","url": "http://xxx.xxx.cn/zorro.mp3"}]})

要输出

{url:"http://xxx.xxx.cn/zorro.mp3",pic:"http://xxx.xxx.cn/zorro.gif",cat:"类别",title:"标题"}
PHP中文网PHP中文网2748 days ago400

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-10 14:26:06

    $.ajax({
                    type:'GET',
                    dataType:'jsonp',
                    jsonp:'jsonp',
                                    jsonCallback:'jsonp',
                    url:'http://fm.izda.com/index.php?a=guessyoulike',
                    success:function(response) {
                                    var infomation = response.songs.data.split('|');
                                    result = {
                                        url: response.songs.url,
                                        pic: infomation[2],
                                        cat: infomation[3],
                                        title: infomation[5].replace(infomation[2], '')
                                              }
                                    $("body").append(result);
                    },
                    error:function() {
                        alert("Sorry, I can't get it"); 
                    }
                });
    

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-10 14:26:06

    这是jsonp吧,在回调函数里头直接按照对象的格式读取就好了。

    function(response) {
        var infomation = response.songs.data.split('|');
        result = {
            url: response.songs.url,
            pic: infomation[2],
            cat: infomation[3],
            title: infomation[5].replace(infomation[2], '')
        }
    }
    

    result就是你想要的结果了。

    reply
    0
  • Cancelreply