0

抱歉,我确定这有一个副本,但我对 jQuery 真的很陌生。我有以下 JSON 字符串...

{
   "totalNumEntries":2,
   "pageType":"CampaignPage",
   "totalBudget":{
      "period":{
         "value":"DAILY"
      },
      "amount":{
         "comparableValueType":"Money",
         "microAmount":0
      },
      "deliveryMethod":null
   },
   "entries":[
      {
         "id":733413,
         "name":"Interplanetary Cruise #1345659006301",
         "status":null,
         "servingStatus":null,
         "startDate":null,
         "endDate":null,
         "budget":null,
         "biddingStrategy":null,
         "conversionOptimizerEligibility":null,
         "campaignStats":{
            "startDate":null,
            "endDate":null,
            "network":{
               "value":"ALL"
            },
            "clicks":null,
            "impressions":null,
            "cost":null,
            "averagePosition":null,
            "averageCpc":null,
            "averageCpm":null,
            "ctr":null,
            "conversions":null,
            "viewThroughConversions":null,
            "statsType":"CampaignStats"
         },
         "adServingOptimizationStatus":null,
         "frequencyCap":{
            "impressions":0,
            "timeUnit":null,
            "level":null
         },
         "settings":null,
         "networkSetting":null,
         "forwardCompatibilityMap":null
      },
      {
         "id":733414,
         "name":"Interplanetary Cruise banner #1345659006387",
         "status":null,
         "servingStatus":null,
         "startDate":null,
         "endDate":null,
         "budget":null,
         "biddingStrategy":null,
         "conversionOptimizerEligibility":null,
         "campaignStats":{
            "startDate":null,
            "endDate":null,
            "network":{
               "value":"ALL"
            },
            "clicks":null,
            "impressions":null,
            "cost":null,
            "averagePosition":null,
            "averageCpc":null,
            "averageCpm":null,
            "ctr":null,
            "conversions":null,
            "viewThroughConversions":null,
            "statsType":"CampaignStats"
         },
         "adServingOptimizationStatus":null,
         "frequencyCap":{
            "impressions":0,
            "timeUnit":null,
            "level":null
         },
         "settings":null,
         "networkSetting":null,
         "forwardCompatibilityMap":null
      }
   ]
}

这是从我的应用程序中的 /google/getCampaigns 返回的。我正在使用以下代码循环浏览它,但页面保持空白......

<script>
$(document).ready(function() {
    $.getJSON('google/getCampaigns', function(data) {
        $.each(data.entries, function(index) {
            $('span').append(index.name);
        });
    });
});

</script>
Loading...
<span />

谁能看到我做错了什么?

谢谢,

大卫

4

2 回答 2

7

每个语句应该是这样的:

$.each(data.entries, function(index,element) {
    $('span').append(element.name);
});

或者另一种方法是

$.each(data.entries, function(index) {
    $('span').append(data.entries[index].name);
});

希望这能解决您的问题。

于 2012-09-01T10:23:50.563 回答
1

传递其他任何东西甚至不是索引的需要是什么,这肯定会帮助你

$.each(data.entries, function() {
      $('span').append(this.name);
});
于 2012-09-01T10:25:09.060 回答