ruby循环遍历API调用返回的数据

时间:2013-12-03 00:23:32

标签: ruby sinatra plivo

我正在和ruby和sinatra一起工作。使用Plivo api,我想访问返回的数据,遍历它并提取并打印一些元素的所有实例,并在我的网页上以HTML格式打印出来。

我的红宝石代码是:

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

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

{
   "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"
      }
   ]
}
抱歉,这太久了。

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

当我尝试:

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

heroku错误是:

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

这里有什么问题?谢谢你们!

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

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

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

2 个答案:

答案 0 :(得分:0)

实际上,.to_json会生成字符串,而不是哈希值,这就是您收到错误的原因。它在http://www.ruby-doc.org/stdlib-2.0/libdoc/json/rdoc/JSON.html#module-JSON-label-Generating+JSON

进行了非正式记录

您可以将该字符串转换回JSON.parse的对象,也可以直接在response.last上操作。但是,在任何一种情况下,请注意该对象将是哈希,而不是数组,因此您必须先从中选择objects元素,然后才能开始迭代对象数组,如:

response.last['objects'].each

JSON.parse(obj)['objects'].each

答案 1 :(得分:0)

在Peter Alfvin的帮助下,sanswer是:

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

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

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

太棒了,多年来我一直在努力奋斗。谢谢Peter !!