雄辩的Ruby - 从哈希中提取值

时间:2013-11-08 17:15:43

标签: ruby hash

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

3 个答案:

答案 0 :(得分: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
  

firstName | dateOfBirth |日期| checkInTime |日期
  | checkInTime |日期| checkInTime |日期|
  checkInTime |日期| checkInTime |日期|登记时间   |日期| checkInTime |日期| 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分○○秒

答案 1 :(得分:0)

我认为这样可行:

first_name = your_hash[:data][0]['firstName']
date = your_hash[:data][0]['details'][0]['date']

答案 2 :(得分:0)

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

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"]
]