使用jQuery的.each()方法解析JSON数组

5
我有这个jQuery函数。
function getData(jsonLink){
  $(".scrollable .items").html("<img class='loadGif' src='/sites/all/themes/zen/journeyon/images/ajax-loader.gif' alt='' />");

  $.ajaxSetup({
    url: jsonLink,
    global: false,
    type: "GET"
  });

  $.ajax({
    url: jsonLink,
    success: function(data) {
      var output = "";
      $.each(data['nodes'], function(i,row){
        var linkType = row['node-type'];
        var lowerLinkType = linkType.toLowerCase();
        var videoLink = row["video"];
        var thumbLink = row["thumbnail"];
        var posterLink = row["poster-image"];
        var nodeTitle = row["node-title"];
        var url = row['url-link'];
        if(linkType == "Episode"){
          output+='<li><a class="myRemote '+lowerLinkType+'" href="'+posterLink+'" ref="'+videoLink+'" title="Play '+nodeTitle+'"><img src="'+thumbLink+'" width="123" height="67" alt="Play '+nodeTitle+'" /></a></li>';
        }else if(linkType == "Slide"){
          output+='<li><a class="myRemote '+lowerLinkType+'" href="'+posterLink+'" ref="'+url+'" title="Go To '+nodeTitle+'"><img src="'+thumbLink+'" width="123" height="67" alt="Go To '+nodeTitle+'" /></a></li>';
        }
      });
      $(".scrollable .items").html("").append(output);
      remoteControl();
    }
  });
}

我正在尝试解析这个数组。
{
    "nodes": [
        {
            "node-title" : "Slide for Album Post",
            "node-type" : "Slide",
            "poster-image" : "http://journeyon.local/sites/default/files/imagecache/ticker_image/files/poster-images/Web_Graphic2.jpg",
            "thumbnail" : "http://journeyon.local/sites/default/files/imagecache/ticker_thumbnail/files/poster-images/Web_Graphic2.jpg",
            "video" : "",
            "audio" : "",
            "url-link" : "http://dev.journeystl.info/current/blogs/josh-dix/latest-worship-ep-have-thine-own-way"
        },
        {
            "node-title" : "Walking In The Light",
            "node-type" : "Episode",
            "poster-image" : "http://journeyon.local/sites/default/files/imagecache/ticker_image/files/poster-images/Sermon_2009-09-27pf.jpg",
            "thumbnail" : "http://journeyon.local/sites/default/files/imagecache/ticker_thumbnail/files/poster-images/Sermon_2009-09-27pf.jpg",
            "video" : "http://journeyon.local/sites/default/files/video/vodcast/Sermon_2009-09-27-Vodcast.m4v",
            "audio" : "http://journeyon.local/sites/default/files/audio/Sermon_2009-09-27-Podcast.mp3",
            "url-link" : ""
        },
        {
            "node-title" : "Test Slide",
            "node-type" : "Slide",
            "poster-image" : "http://journeyon.local/sites/default/files/imagecache/ticker_image/files/poster-images/iStock_000000041926Small.jpg",
            "thumbnail" : "http://journeyon.local/sites/default/files/imagecache/ticker_thumbnail/files/poster-images/iStock_000000041926Small.jpg",
            "video" : "",
            "audio" : "",
            "url-link" : "/node/3960"
        },
        {
            "node-title" : "Finding God at Church",
            "node-type" : "Episode",
            "poster-image" : "http://journeyon.local/sites/default/files/imagecache/ticker_image/files/poster-images/Sermon_2009-09-06pf_0.jpg",
            "thumbnail" : "http://journeyon.local/sites/default/files/imagecache/ticker_thumbnail/files/poster-images/Sermon_2009-09-06pf_0.jpg",
            "video" : "http://journeyon.local/sites/default/files/video/vodcast/Sermon_2009-09-05-Vodcast.m4v",
            "audio" : "http://journeyon.local/sites/default/files/audio/Sermon_2009-09-06-Podcast.mp3",
            "url-link" : ""
        },
        {
            "node-title" : "Finding God in Brokenness",
            "node-type" : "Episode",
            "poster-image" : "http://journeyon.local/sites/default/files/imagecache/ticker_image/files/poster-images/Sermon_2009-08-30pf_0.jpg",
            "thumbnail" : "http://journeyon.local/sites/default/files/imagecache/ticker_thumbnail/files/poster-images/Sermon_2009-08-30pf_0.jpg",
            "video" : "http://journeyon.local/sites/default/files/video/vodcast/Sermon_2009-08-30-Vodcast.m4v",
            "audio" : "http://journeyon.local/sites/default/files/audio/Sermon_2009-08-30-Podcast.mp3",
            "url-link" : ""
        },
        {
            "node-title" : "False Teachers",
            "node-type" : "Episode",
            "poster-image" : "http://journeyon.local/sites/default/files/imagecache/ticker_image/files/poster-images/Sermon_2009-07-26pf.jpg",
            "thumbnail" : "http://journeyon.local/sites/default/files/imagecache/ticker_thumbnail/files/poster-images/Sermon_2009-07-26pf.jpg",
            "video" : "http://journeyon.local/sites/default/files/video/vodcast/Sermon_2009-07-25-Vodcast.m4v",
            "audio" : "http://journeyon.local/sites/default/files/audio/Sermon_2009-07-25_Podcast.mp3",
            "url-link" : ""
        },
        {
            "node-title" : "Confessions: A Story of Struggle, Restoration, and Hope",
            "node-type" : "Episode",
            "poster-image" : "http://journeyon.local/sites/default/files/imagecache/ticker_image/files/poster-images/PosterFrame_Confessions.jpg",
            "thumbnail" : "http://journeyon.local/sites/default/files/imagecache/ticker_thumbnail/files/poster-images/PosterFrame_Confessions.jpg",
            "video" : "http://journeyon.local/sites/default/files/video/vodcast/Sermon_2009-06-28-Vodcast.m4v",
            "audio" : "http://journeyon.local/sites/default/files/audio/Sermon_2009-06-28-Podcast.mp3",
            "url-link" : ""
        } 
    ] 
} 

问题是我在jQuery.js文件本身上遇到了一个错误。
G is undefined
http://journeyon.local/sites/all/themes/zen/journeyon/js/jquery-1.3.2.min.js?4
Line 12

有人知道发生了什么吗? 如果我在.each()语句之前使用alert,它可以正常弹出,但是如果我在.each()语句内部使用alert,我什么也看不到,变量也没有被建立。

谢谢!

4个回答

7
$.ajax({
 url: jsonLink,
 dataType: 'json',
 success: function(data) {
  $.each(data.items, function(i,item){
  // your code ..
  });
 }
});

3

您使用$.ajax()是一种较为繁琐的方式。相比之下,使用$.getJSON()会更加简便。


很有趣,因为IE6根本不喜欢getJSON,至少使用ajax调用时,IE6会在成功时发出警报。 IE6从未使用getJSON命中“成功”。我与此纠缠了8-10个小时,然后切换到.ajax并立即获得成功警报...现在它只是无法构建列表项。 - elDeuce
1
jQuery在隔离浏览器差异方面做得很好,但如果您编写依赖于浏览器实现细节的代码,它将无法拯救您自己。举个例子:[1, 2, 3,].length在所有浏览器上都是3,但在IE上是4,最后一个元素是“undefined”。我的意思是,如果$.getJSON()不起作用,您可能正在做错某些事情。我建议在Firefox上使用Firebug,在IE8上使用F12工具运行程序。其中之一可能会标记您代码中的错误,修复后,代码就可以在IE6上运行了。 - Warren Young
好吧,你逼我回到getJSON并且它奏效了!我应该再试一次的!我的原始问题是一个格式不正确的JSON数组,Safari和Firefox比IE更容易处理...在验证了该数组之后,我应该再次尝试使用getJSON... - elDeuce

1

对于以 PHP 数组形式进行 JSON 编码的数据

<?php $arr= array('1'=>'2', 2=>'3',4000=>'400','x'=>'alfa'); ?>

<input type="hidden" value="<?php echo json_econde($arr); ?>" id="phpjsonencoded">

你可以使用

标签


var x = "[" + $('#phpjsonencoded').val() + "]";

var obj = $.parseJSON(x);

$.each(obj[0],function(key,val){

console.log(val);

});

0

只需使用row.node-typerow.video等,而不是使用row['node-type']row["video"]等。

希望这能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接