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:"标题"}
阅读 4.4k
2 个回答
$.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"); 
                }
            });

这是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就是你想要的结果了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题