遍历jQuery JSON Object数组

我尝试使用getJson访问一个Object数组,我尝试了很多东西,但我一直得到一个’undefined’或[Object,object]返回。

$.getJSON( "js/test.json", function( data ) { var items = new Array(); $.each( data, function( key, val ) { items.push( "
  • " + val.entries.title + "
  • " ); }); $( "
      ", { "class": "my-new-list", html: items.join( "" ) }).appendTo( "body" ); });

    这是JSON,我试图获得每个“条目”的“标题”。

     { "$xmlns": { "pl1": "url" }, "startIndex": 1, "author": "MJS", "entries": [ { "title": "This is target", "m$ct": [ { "m$name": "name" } ], "m$rt": "pd", "m$content": [ { "plfile$width": 640 }, { "plfile$width": 960 } ], "plm$c": [], "link": "" }, { "title": "title2", "m$ct": [ { "m$name": "name" } ], "m$rt": "pd", "m$content": [ { "plfile$width": 640 }, { "plfile$width": 960 } ], "plm$c": [], "link": "" } ] } 

     $.each( data, function( key, val ) { items.push( "
  • " + val.entries.title + "
  • " ); });

    应改为:

     $.each( data.entries, function( key, entry ) { items.push( "
  • " + entry.title + "
  • " ); });

    你正在迭代整个json而不是条目

    它应该是每个中的data.entries然后只是val.title

     $.getJSON( "js/test.json", function( data ) { var items = new Array(); $.each( data.entries, function( key, val ) { items.push( "
  • " + val.title + "
  • " ); }); $( "
      ", { "class": "my-new-list", html: items.join( "" ) }).appendTo( "body" ); });