红宝石循环遍历API调用返回的数据 [英] ruby loop through data returned by API call

查看:87
本文介绍了红宝石循环遍历API调用返回的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ruby和sinatra.使用Plivo api,我想访问返回的数据,对其进行迭代,提取并打印某些元素的所有实例,然后将它们打印为HTML格式在我的网页上.

I am working with ruby and sinatra. Using the Plivo api, I want to access the returned data, iterate through it and extract and print all the instances of some of the elements and print them out as HTML on my web page.

我的红宝石代码是:

user_key = 'my_key'
 params = { 'account' => user_key }
 response = p.get_cdrs(params)
 obj = response.last.to_json

在Firebug控制台中返回以下内容:

which gets the following returned in the Firebug console:

{
   "api_id":"c59f2008-5baf-11e3-bbb2-22000ac522d1",
   "meta":{
      "limit":20,
      "next":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/?account=MAMTE4MTHJNJRKODBIMD&limit=20&offset=20",
      "offset":0,
      "previous":null,
      "total_count":114
   },
   "objects":[
      {
         "bill_duration":21,
         "billed_duration":60,
         "call_direction":"outbound",
         "call_duration":21,
         "call_uuid":"103a3bc6-5baa-11e3-a878-f30f0e86751e",
         "end_time":"2013-12-03 08:33:44+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"0313c23c-5baa-11e3-a673-f30f0e86751e",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/103a3bc6-5baa-11e3-a878-f30f0e86751e/",
         "to_number":"14157638148",
         "total_amount":"0.01200",
         "total_rate":"0.01200"
      },
      {
         "bill_duration":21,
         "billed_duration":60,
         "call_direction":"inbound",
         "call_duration":21,
         "call_uuid":"0313c23c-5baa-11e3-a673-f30f0e86751e",
         "end_time":"2013-12-03 08:33:44+09:00",
         "from_number":"sip:hechan131129083410@phone.plivo.com",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/0313c23c-5baa-11e3-a673-f30f0e86751e/",
         "to_number":"14157638148",
         "total_amount":"0.00300",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":358,
         "billed_duration":360,
         "call_direction":"inbound",
         "call_duration":358,
         "call_uuid":"0b07abb2-5b40-11e3-8fb1-057cf5362a5f",
         "end_time":"2013-12-02 19:59:58+09:00",
         "from_number":"441903773807",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/0b07abb2-5b40-11e3-8fb1-057cf5362a5f/",
         "to_number":"441903337012",
         "total_amount":"0.04800",
         "total_rate":"0.00800"
      },
      {
         "bill_duration":344,
         "billed_duration":360,
         "call_direction":"outbound",
         "call_duration":344,
         "call_uuid":"116d89f4-5b40-11e3-8fe5-057cf5362a5f",
         "end_time":"2013-12-02 19:59:57+09:00",
         "from_number":null,
         "parent_call_uuid":"0b07abb2-5b40-11e3-8fb1-057cf5362a5f",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/116d89f4-5b40-11e3-8fe5-057cf5362a5f/",
         "to_number":"sip:niyan130903202713@phone.plivo.com",
         "total_amount":"0.01800",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":27,
         "billed_duration":60,
         "call_direction":"outbound",
         "call_duration":27,
         "call_uuid":"1b026a9a-5b3e-11e3-898e-1b5c95282229",
         "end_time":"2013-12-02 19:40:46+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"19f8572c-5b3e-11e3-8930-1b5c95282229",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/1b026a9a-5b3e-11e3-898e-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.01900",
         "total_rate":"0.01900"
      },
      {
         "bill_duration":27,
         "billed_duration":60,
         "call_direction":"inbound",
         "call_duration":27,
         "call_uuid":"19f8572c-5b3e-11e3-8930-1b5c95282229",
         "end_time":"2013-12-02 19:40:46+09:00",
         "from_number":"sip:hechan131129083410@phone.plivo.com",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/19f8572c-5b3e-11e3-8930-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.00300",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":175,
         "billed_duration":180,
         "call_direction":"inbound",
         "call_duration":175,
         "call_uuid":"e59e46f6-5b3a-11e3-8488-1b5c95282229",
         "end_time":"2013-12-02 19:20:16+09:00",
         "from_number":"sip:hechan131129083410@phone.plivo.com",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/e59e46f6-5b3a-11e3-8488-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.00900",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":175,
         "billed_duration":180,
         "call_direction":"outbound",
         "call_duration":175,
         "call_uuid":"e698b528-5b3a-11e3-84e1-1b5c95282229",
         "end_time":"2013-12-02 19:20:16+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"e59e46f6-5b3a-11e3-8488-1b5c95282229",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/e698b528-5b3a-11e3-84e1-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.05700",
         "total_rate":"0.01900"
      },
      {
         "bill_duration":728,
         "billed_duration":780,
         "call_direction":"outbound",
         "call_duration":728,
         "call_uuid":"b421ca72-5b34-11e3-82c5-1b5c95282229",
         "end_time":"2013-12-02 18:45:04+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"b3210c64-5b34-11e3-8250-1b5c95282229",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/b421ca72-5b34-11e3-82c5-1b5c95282229/",
         "to_number":"85265001927",
         "total_amount":"0.33800",
         "total_rate":"0.02600"
      },
      {
         "bill_duration":728,
         "billed_duration":780,
         "call_direction":"inbound",
         "call_duration":728,
         "call_uuid":"b3210c64-5b34-11e3-8250-1b5c95282229",
         "end_time":"2013-12-02 18:45:04+09:00",
         "from_number":"sip:hechan131129083410@phone.plivo.com",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/b3210c64-5b34-11e3-8250-1b5c95282229/",
         "to_number":"85265001927",
         "total_amount":"0.03900",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":800,
         "billed_duration":840,
         "call_direction":"outbound",
         "call_duration":800,
         "call_uuid":"49c4bc6a-5b30-11e3-aa2e-f30f0e86751e",
         "end_time":"2013-12-02 18:14:41+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"43465b3c-5b30-11e3-a842-f30f0e86751e",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/49c4bc6a-5b30-11e3-aa2e-f30f0e86751e/",
         "to_number":"818042010550",
         "total_amount":"2.01600",
         "total_rate":"0.14400"
      },
      {
         "bill_duration":800,
         "billed_duration":840,
         "call_direction":"inbound",
         "call_duration":800,
         "call_uuid":"43465b3c-5b30-11e3-a842-f30f0e86751e",
         "end_time":"2013-12-02 18:14:41+09:00",
         "from_number":"sip:hechan131129083410@phone.plivo.com",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/43465b3c-5b30-11e3-a842-f30f0e86751e/",
         "to_number":"818042010550",
         "total_amount":"0.04200",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":2,
         "billed_duration":60,
         "call_direction":"outbound",
         "call_duration":2,
         "call_uuid":"7f1db12e-5a7b-11e3-9c03-c7a22946c980",
         "end_time":"2013-12-01 20:27:08+09:00",
         "from_number":"185258081345",
         "parent_call_uuid":"7e6225d0-5a7b-11e3-9bd4-c7a22946c980",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/7f1db12e-5a7b-11e3-9c03-c7a22946c980/",
         "to_number":"sip:hechan131129083410@phone.plivo.com",
         "total_amount":"0.00300",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":2,
         "billed_duration":60,
         "call_direction":"inbound",
         "call_duration":2,
         "call_uuid":"7e6225d0-5a7b-11e3-9bd4-c7a22946c980",
         "end_time":"2013-12-01 20:27:08+09:00",
         "from_number":"185258081345",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/7e6225d0-5a7b-11e3-9bd4-c7a22946c980/",
         "to_number":"14154292111",
         "total_amount":"0.00850",
         "total_rate":"0.00850"
      },
      {
         "bill_duration":1628,
         "billed_duration":1680,
         "call_direction":"outbound",
         "call_duration":1628,
         "call_uuid":"108c1c14-5a76-11e3-a749-1b5c95282229",
         "end_time":"2013-12-01 20:15:25+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"0f78d9c0-5a76-11e3-a6af-1b5c95282229",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/108c1c14-5a76-11e3-a749-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.53200",
         "total_rate":"0.01900"
      },
      {
         "bill_duration":1628,
         "billed_duration":1680,
         "call_direction":"inbound",
         "call_duration":1628,
         "call_uuid":"0f78d9c0-5a76-11e3-a6af-1b5c95282229",
         "end_time":"2013-12-01 20:15:25+09:00",
         "from_number":"sip:hechan131129083410@phone.plivo.com",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/0f78d9c0-5a76-11e3-a6af-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.08400",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":57,
         "billed_duration":60,
         "call_direction":"outbound",
         "call_duration":57,
         "call_uuid":"ce7f25f6-5a6f-11e3-ae45-f30f0e86751e",
         "end_time":"2013-12-01 19:04:37+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"c938e38e-5a6f-11e3-aca3-f30f0e86751e",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/ce7f25f6-5a6f-11e3-ae45-f30f0e86751e/",
         "to_number":"441903773807",
         "total_amount":"0.01900",
         "total_rate":"0.01900"
      },
      {
         "bill_duration":57,
         "billed_duration":60,
         "call_direction":"inbound",
         "call_duration":57,
         "call_uuid":"c938e38e-5a6f-11e3-aca3-f30f0e86751e",
         "end_time":"2013-12-01 19:04:37+09:00",
         "from_number":"sip:hechan131129083410@phone.plivo.com",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/c938e38e-5a6f-11e3-aca3-f30f0e86751e/",
         "to_number":"441903773807",
         "total_amount":"0.00300",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":126,
         "billed_duration":180,
         "call_direction":"outbound",
         "call_duration":126,
         "call_uuid":"24ed88a4-5a59-11e3-a462-fdc08976d83f",
         "end_time":"2013-12-01 16:23:39+09:00",
         "from_number":"185258081345",
         "parent_call_uuid":"2445b890-5a59-11e3-a437-fdc08976d83f",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/24ed88a4-5a59-11e3-a462-fdc08976d83f/",
         "to_number":"818088931617",
         "total_amount":"0.43200",
         "total_rate":"0.14400"
      },
      {
         "bill_duration":126,
         "billed_duration":180,
         "call_direction":"inbound",
         "call_duration":126,
         "call_uuid":"2445b890-5a59-11e3-a437-fdc08976d83f",
         "end_time":"2013-12-01 16:23:39+09:00",
         "from_number":"185258081345",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/2445b890-5a59-11e3-a437-fdc08976d83f/",
         "to_number":"14154292111",
         "total_amount":"0.02550",
         "total_rate":"0.00850"
      }
   ]
}

很抱歉,时间太长了.

sorry it is so long.

现在,我想访问数据的某些部分并以HTML格式打印出来,例如,所有bill_durations和所有call_directions.我怎样才能做到这一点?

Now, I want to access certain parts of the data and print out as HTML, for example, all the bill_durations and all the call_directions. How can I do this?

当我尝试时:

obj.each do |row|
   bill_duration = ob["objects"][0]["bill_duration"]
  halt 200, { :bill_duration => bill_duration }
  end

heroku错误是:

the heroku error is:

NoMethodError - undefined method `each' for #<String:0x007f82e6448b38>

这是怎么了?谢谢大家!

What's wrong here? Thanks guys!

好的,这就是我现在拥有的:

OK, here is what I have now:

response.last['objects'].each do |row| 
 bill_duration = row["bill_duration"] 
 halt 200,  { :bill_duration => bill_duration }.to_json 
 end

这不会产生错误,但是尽管循环,它只会返回一条记录.我需要获取bill_duration的所有实例.

this does not produce an error but it only returns one record despite the loop. I need to get all the instances of bill_duration.

推荐答案

在彼得·阿尔夫文(Peter Alfvin)的帮助下,答案是:

with assistance from Peter Alfvin, the sanswer is:

results =  response.last['objects'].each do |row| 
  bill_duration = row["bill_duration"]
  { :bill_duration => bill_duration }
  end
 halt 200,  results.to_json 
 end

然后可以使用jQuery进行访问并打印出来,如下所示:

then can be accessed and printed out, with jQuery, as follows:

$.getJSON("/callrecords", function(data) {
        $.each( data, function( key, value ) {
        var txt = '<p>' +this["bill_duration"]+ '</p>';
      $(".results").append(txt);
     });//end each
    });//end JSON

太棒了,我一直在努力地尝试这个问题.谢谢彼得!

Brilliant, I have been struggling with this, on and off, for ages. Thank you Peter!!

这篇关于红宝石循环遍历API调用返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆