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

时间:2022-09-25 15:31:11

I have this jQuery function

我有这个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();
    }
  });
}

And I am trying to parse out this Array

我试图解析这个数组

{
    "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" : ""
        } 
    ] 
} 

Problem is I am getting an error on the jQuery.js file itself.

问题是我在jQuery.js文件本身上收到错误。

G is undefined
http://journeyon.local/sites/all/themes/zen/journeyon/js/jquery-1.3.2.min.js?4
Line 12

Anyone have any clue what's going on? if I alert before the .each() statement it alerts just fine, but if I alert inside the .each() statement I get nothing and the variables never get built.

有人知道发生了什么事吗?如果我在.each()语句之前发出警报就会发出警报,但如果我在.each()语句中发出警报,我什么也得不到,变量永远不会被构建。

Thanks!

4 个解决方案

#1


3  

You're doing it the hard way by using $.ajax(). It's easier to use $.getJSON() instead.

你正在通过使用$ .ajax()来做到这一点。使用$ .getJSON()更容易。

#2


6  

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

#3


1  

For a PHP array json encoded

对于PHP数组json编码

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

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

you can use

您可以使用

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

var obj = $.parseJSON(x);

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

console.log(val);

});

#4


0  

Just use row.node-type, row.video etc instead of using row['node-type'], row["video"] etc.

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

Hope this will help.

希望这会有所帮助。

#1


3  

You're doing it the hard way by using $.ajax(). It's easier to use $.getJSON() instead.

你正在通过使用$ .ajax()来做到这一点。使用$ .getJSON()更容易。

#2


6  

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

#3


1  

For a PHP array json encoded

对于PHP数组json编码

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

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

you can use

您可以使用

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

var obj = $.parseJSON(x);

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

console.log(val);

});

#4


0  

Just use row.node-type, row.video etc instead of using row['node-type'], row["video"] etc.

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

Hope this will help.

希望这会有所帮助。