我从 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
对象应该是该行上的单独列。