0

我从 API GET 请求中收到以下响应:

{"data" => [
  {"firstName"=>"Test", 
   "lastName"=>"LastName", 
   "dateOfBirth"=>"2003-01-17", 
   "details"=> [
     {"date"=>"2013-10-01T00:00:00", 
      "type"=>1, 
      "checkInTime"=>"07:00:00", 
      "checkOutTime"=>"08:00:00"},
     {"date"=>"2013-10-01T00:00:00", 
       "type"=>2, 
       "checkInTime"=>"15:30:00", 
       "checkOutTime"=>"16:00:00"}, 
     {"date"=>"2013-10-02T00:00:00", 
      "type"=>1, 
      "checkInTime"=>"07:00:00", 
      "checkOutTime"=>"08:00:00"}, 
     {"date"=>"2013-10-02T00:00:00", 
      "type"=>2, 
      "checkInTime"=>"15:30:00", 
      "checkOutTime"=>"16:00:00"}, 
     {"date"=>"2013-10-03T00:00:00", 
      "type"=>1, 
      "checkInTime"=>"07:00:00", 
      "checkOutTime"=>"08:00:00"}, 
     {"date"=>"2013-10-04T00:00:00", 
      "type"=>1, 
      "checkInTime"=>"07:00:00", 
      "checkOutTime"=>"08:00:00"}, 
     {"date"=>"2013-10-07T00:00:00", 
      "type"=>1,
      "checkInTime"=>"07:00:00", 
      "checkOutTime"=>"08:00:00"}, 
     {"date"=>"2013-10-07T00:00:00",
      "type"=>2,
      "checkInTime"=>"15:30:00",
      "checkOutTime"=>"16:40:00"}]}]}

我将收到 100~ 个类似的回复。我需要将此信息提取到表中的行中。

我目前的方法是将每个键/值转储到一个单独的数组中,并将数组转置为列,本质上是创建行。不管这是否正确,我怎样才能用上面的哈希提取这样的东西:

First Name | Date Of Birth | Date       | Check In Time| Date       | Check In Time| ...
Test       | 2003-01-07    | 2013-10-01 | 07:00        | 2013-10-01 | 15:30        | ...

附带说明一下,我将进行某种循环,并有大约 100 行具有相似数据的行。

编辑:

为了提供更多信息,我正在使用 Prawn gem 在 PDF 中生成一个表格。代码如下所示:

font("Times-Roman", :size => 8) do
  bounding_box([bounds.left, bounds.top - 165], :width  => bounds.width, :height => bounds.height - 200) do                 
table([
          [{content: "Child's Name",borders: [:left, :top]},{:content => "Sunday", :align => :center, :colspan => 2},{:content => "Monday", :align => :center, :colspan => 2},{:content => "Tuesday", :align => :center, :colspan => 2},{:content => "Wednesday", :align => :center, :colspan => 2},{:content => "Thursday", :align => :center, :colspan => 2},{:content => "Friday", :align => :center, :colspan => 2},{:content => "Saturday", :align => :center, :colspan => 2},{content: 'Signature of Parent or Designated Person to Verify Accuracy of Attendance for the week', rowspan: 3}],
          [{content: '', :height => 20,borders: [:left]},'Time In', 'Time Out', 'Time In', 'Time Out', 'Time In', 'Time Out', 'Time In', 'Time Out', 'Time In', 'Time Out', 'Time In', 'Time Out', 'Time In', 'Time Out'],
          [{content: '(as it appears on PBF)', :height => 26,borders: [:left, :bottom], align: :center, inline_format: true }, 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials', 'Initials']
        ],
          #Set column widths
        :column_widths => {0 => 60,1 => 42,2 => 42,3 => 42,4 => 42,5 => 42,6 => 42,7 => 42,8 => 42,9 => 42,10 => 42,11 => 42,12 => 42,13 => 42,14 => 42}) do
        #Color every other column grey --------------
        column(1).style :background_color => 'C0C0C0'
        column(2).style :background_color => 'C0C0C0'
        column(5).style :background_color => 'C0C0C0'
        column(6).style :background_color => 'C0C0C0'
        column(9).style :background_color => 'C0C0C0'
        column(10).style :background_color => 'C0C0C0'
        column(13).style :background_color => 'C0C0C0'
        column(14).style :background_color => 'C0C0C0'
      end #End Column Style
    end #End Boundind Box
  end #End Font

该表生成正确,但它只包含标题。我从 GET 请求收到的响应应该适合此表。每个data元素应该是表中的单独行,每个details对象应该是该行上的单独列。

4

3 回答 3

0

对我来说,快速浏览 IRB 表明这确实(我认为)你想要它......

hash = {"data" => [
      {"firstName"=>"Test", 
       "lastName"=>"LastName", 
       "dateOfBirth"=>"2003-01-17", 
       "details"=> [
         {"date"=>"2013-10-01T00:00:00", 
          "type"=>1, 
          "checkInTime"=>"07:00:00", 
          "checkOutTime"=>"08:00:00"},
         {"date"=>"2013-10-01T00:00:00", 
           "type"=>2, 
           "checkInTime"=>"15:30:00", 
           "checkOutTime"=>"16:00:00"}, 
         {"date"=>"2013-10-02T00:00:00", 
          "type"=>1, 
          "checkInTime"=>"07:00:00", 
          "checkOutTime"=>"08:00:00"}, 
         {"date"=>"2013-10-02T00:00:00", 
          "type"=>2, 
          "checkInTime"=>"15:30:00", 
          "checkOutTime"=>"16:00:00"}, 
         {"date"=>"2013-10-03T00:00:00", 
          "type"=>1, 
          "checkInTime"=>"07:00:00", 
          "checkOutTime"=>"08:00:00"}, 
         {"date"=>"2013-10-04T00:00:00", 
          "type"=>1, 
          "checkInTime"=>"07:00:00", 
          "checkOutTime"=>"08:00:00"}, 
         {"date"=>"2013-10-07T00:00:00", 
          "type"=>1,
          "checkInTime"=>"07:00:00", 
          "checkOutTime"=>"08:00:00"}, 
         {"date"=>"2013-10-07T00:00:00",
          "type"=>2,
          "checkInTime"=>"15:30:00",
          "checkOutTime"=>"16:40:00"}]}]}


@results_set = hash["data"]

@results_set.each do |results_hash|
  header_row = ["firstName", "dateOfBirth"]
  results_row = [results_hash["firstName"], results_hash["lastName"]]

  results_hash["details"].each do |details_hash|
    header_row << ["date", "checkInTime"]
    results_row << [details_hash["date"], details_hash["checkInTime"]]
  end

  puts header_row.flatten.join("   |   ")
  puts results_row.flatten.join("   |   ")
end;nil

名字 | 出生日期 | 日期 | 签到时间 | 日期
| 签到时间 | 日期 | 签到时间 | 日期 |
签到时间 | 日期 | 签到时间 | 日期 | 签到时间 | 日期 | 签到时间 | 日期 | checkInTime 测试 |
姓氏 | 2013-10-01T00:00:00 | 07:00:00 |
2013-10-01T00:00:00 | 15:30:00 | 2013-10-02T00:00:00 |
07:00:00 | 2013-10-02T00:00:00 | 15:30:00 |
2013-10-03T00:00:00 | 07:00:00 | 2013-10-04T00:00:00 |
07:00:00 | 2013-10-07T00:00:00 | 07:00:00 |
2013-10-07T00:00:00 | 15:30:00

于 2013-11-08T17:26:57.187 回答
0

I think this would work:

first_name = your_hash[:data][0]['firstName']
date = your_hash[:data][0]['details'][0]['date']
于 2013-11-08T17:28:42.557 回答
0

我仍然不能 100% 确定你在寻找什么,但如果你想要的只是从数据中的每个条目中提取代表一行的数据数组,我可能会做这样的事情:

data = your_hash["data"]
result = data.map do |entry|
  [entry["firstName"], entry["lastName"], entry["dateOfBirth"]] + entry["details"].flat_map do |details|
    [details["checkInTime"], details["checkOutTime"]]
  end
end

在你的情况下产生:

[
  ["Test", "LastName", "2003-01-17", "07:00:00", "08:00:00", "15:30:00", "16:00:00", "07:00:00", "08:00:00", "15:30:00", "16:00:00", "07:00:00", "08:00:00", "07:00:00", "08:00:00", "07:00:00", "08:00:00", "15:30:00", "16:40:00"]
]
于 2013-11-08T19:02:27.757 回答