0

我正在与一家名为 LogMyCalls 的公司一起使用 API,我们正在尝试提取最近的 5 个调用,并将它们显示在列表中,但我遇到了问题。以下是 json 是如何从它们中出来的:

{"status":"success","matches":3,"results":[{"tracking_number":"8556979885","disposition":"ANSWERED","duration":70,"external_id":null,"id":4918363,"is_outbound":0,"file_url":"http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_6024711995_20130801-214811.mp3?AWSAccessKeyId=**********&Expires=1378497578&Signature=*******","ouid":14104,"caller_id":"**********","calldate":"2013-08-01 21:48:11","status":"active","ringto_number":"**********","title":null,"user_id":null,"scored":false,"tagged":false},{"tracking_number":"8556979885","disposition":"ANSWERED","duration":49,"external_id":null,"id":4916465,"is_outbound":0,"file_url":"http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_5153091261_20130801-203535.mp3?AWSAccessKeyId=**********&Expires=1378497578&Signature=*******","ouid":14104,"caller_id":"**********","calldate":"2013-08-01 20:35:35","status":"active","ringto_number":"**********","title":null,"user_id":null,"scored":false,"tagged":false},{"tracking_number":"8556979885","disposition":"ANSWERED","duration":626,"external_id":null,"id":4911038,"is_outbound":0,"file_url":"http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_6028812248_20130801-162317.mp3?AWSAccessKeyId=**********&Expires=1378497578&Signature=*******","ouid":14104,"caller_id":"**********","calldate":"2013-08-01 16:23:17","status":"active","ringto_number":"**********","title":null,"user_id":null,"scored":false,"tagged":false}],"transaction_id":**********}

这是看起来更漂亮的json:

Array
(
[status] => success
[matches] => 3
[results] => Array
    (
        [0] => Array
            (
                [tracking_number] => 8556979885
                [disposition] => ANSWERED
                [duration] => 70
                [external_id] => 
                [id] => 4918363
                [is_outbound] => 0
                [file_url] => http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_6024711995_20130801-214811.mp3?AWSAccessKeyId=***********&Expires=1378497836&Signature=*******
                [ouid] => 14104
                [caller_id] => *********
                [calldate] => 2013-08-01 21:48:11
                [status] => active
                [ringto_number] => **********
                [title] => 
                [user_id] => 
                [scored] => 
                [tagged] => 
            )

        [1] => Array
            (
                [tracking_number] => 8556979885
                [disposition] => ANSWERED
                [duration] => 49
                [external_id] => 
                [id] => 4916465
                [is_outbound] => 0
                [file_url] => http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_5153091261_20130801-203535.mp3?AWSAccessKeyId=********&Expires=1378497837&Signature=*******
                [ouid] => 14104
                [caller_id] => ******
                [calldate] => 2013-08-01 20:35:35
                [status] => active
                [ringto_number] => **********
                [title] => 
                [user_id] => 
                [scored] => 
                [tagged] => 
            )

        [2] => Array
            (
                [tracking_number] => 8556979885
                [disposition] => ANSWERED
                [duration] => 626
                [external_id] => 
                [id] => 4911038
                [is_outbound] => 0
                [file_url] => http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_6028812248_20130801-162317.mp3?AWSAccessKeyId=*****&Expires=1378497837&Signature=*******
                [ouid] => 14104
                [caller_id] => ***********
                [calldate] => 2013-08-01 16:23:17
                [status] => active
                [ringto_number] => **********
                [title] => 
                [user_id] => 
                [scored] => 
                [tagged] => 
            )

    )

    [transaction_id] => *****
)

我现在使用的是 jQuery 中的以下内容,我能够提取第一个结果 ([0]) 但无论我怎么想,都无法提取其余的结果。

function loadCalls(date1, id){
      $('#calltitle').empty();
      $('#calls').empty();
     $.getJSON("**********", 
          function(data) {

             var calldate = date1.toString('MMMM yyyy');
              $('#calltitle').html('Calls from ' + calldate);

              var callnum = 0;

              $.each(data['results'], function() {                
                  //if(data['results'][callnum].disposition == "ANSWERED") { var icon = 'icon-check'; } else { var icon = 'icon-check-empty'; };          
                  $('#calls').append('<li><div class="col1"><div class="cont"><div class="cont-col1"><div class="label label-info"><i class="' + 'icon-check' + '"></i></div></div><div class="cont-col2"><div class="desc">Call from ' + data['results'][callnum].caller_id + ' at ' + data['results'][callnum].calldate.toString('MMMM dd') + '</div></div></div></div><div class="col2"><div class="date">Just now</div></div></li>');
                  var callnum = callnum++;
              })
          }); 
  }

很抱歉,我不得不*了这么多,但为了客户和我们,我们不得不这样做。

基本上,在我的 jQuery 中,我需要一种方法来提取以下所有结果:

data['results'][0]
data['results'][1]
data['results'][2]
data['results'][3]
data['results'][4]

我最多允许显示 5 个,上面是数组中它们的路由,但我无法增加数字(例如,我尝试在 JS 中设置 var callnum = 0; 和通过使用 var callnum = callnum++; 让它每轮增加一次(理论上应该有效)但它没有用。)

任何帮助表示赞赏!:)

4

1 回答 1

0

当你这样做时var callnum = callnum++;,你基本上会重新分配它。

尝试 :

function loadCalls(date1, id){
      $('#calltitle').empty();
      $('#calls').empty();
     $.getJSON("**********", 
          function(data) {

             var calldate = date1.toString('MMMM yyyy');
              $('#calltitle').html('Calls from ' + calldate);

              var callnum = 0;

              $.each(data['results'], function() {                
                  //if(data['results'][callnum].disposition == "ANSWERED") { var icon = 'icon-check'; } else { var icon = 'icon-check-empty'; };          
                  $('#calls').append('<li><div class="col1"><div class="cont"><div class="cont-col1"><div class="label label-info"><i class="' + 'icon-check' + '"></i></div></div><div class="cont-col2"><div class="desc">Call from ' + data['results'][callnum].caller_id + ' at ' + data['results'][callnum].calldate.toString('MMMM dd') + '</div></div></div></div><div class="col2"><div class="date">Just now</div></div></li>');
                  callnum++;
              })
          }); 
  }
于 2013-09-06T16:21:13.230 回答