使用rails渲染json更好

时间:2016-06-02 12:30:29

标签: javascript ruby-on-rails json

所以我自己设法做到了这一点。我现在有这个渲染

{
  "ticket": [
    [
      "formatted_total_price",
      "£66.00"
    ],
    [
      "formatted_total_price",
      "£128.00"
    ],
    [
      "formatted_total_price",
      "£246.00"
    ],
    [
      "formatted_total_price",
      "£243.20"
    ],
    [
      "formatted_total_price",
      "£242.86"
    ],
    [
      "formatted_total_price",
      "£242.50"
    ],
    [
      "ticket_desc",
      "Later Owl Ticket"
    ],
    [
      "ticket_desc",
      "Later Owl Ticket+Collector Ticket &#64 extra £4.95 per ticket"
    ],
    [
      "ticket_desc",
      "Later Owl + Chance For VIP Upgrade"
    ],
    [
      "ticket_desc",
      "VIP Ticket"
    ],
    [
      "ticket_desc",
      "VIP Ticket + Collector Ticket &#64 extra £4.95 per ticket"
    ],
    [
      "ticket_desc",
      "Skydeck Package"
    ],
    [
      "ticket_desc",
      "5 Person Skydeck Table"
    ],
    [
      "ticket_desc",
      "7 Person Skydeck Table"
    ],
    [
      "ticket_desc",
      "10 Person Skydeck Table"
    ]
  ]}

现在我理想的是让它看起来像这样

 {
      "ticket": [
        [
          "formatted_total_price" : "£66.00"
          "ticket_desc" : "Later Owl Ticket"
        ],
        [
          "formatted_total_price" : "£128.00"
          "ticket_desc" : "VIP Ticket"
        ],
        [
          "formatted_total_price" : "£246.00"
          "ticket_desc" : "Skydeck Package"
        ],
        [
          "formatted_total_price" : "£243.20"
          "ticket_desc" : "5 Person Skydeck Table"
        ],
        [
          "formatted_total_price" : "£242.86"
          "ticket_desc" : "7 Person Skydeck Table"
        ],
        [
          "formatted_total_price" : "£242.50"
          "ticket_desc" : "10 Person Skydeck Table"
        ],

我也希望忽略任何带有+符号的东西。正如你所看到的,我理想的梦想回应已被删除。

这是我目前拥有的代码:

 doc.xpath("//script[@type='text/javascript']/text()").each do |text|
       if text.content =~ /more_options_on_polling/
         byebug
         price1 = text.to_s.scan(/\"(formatted_(?:total_price))\":\"(.+?)\"/).uniq
         description = text.to_s.scan(/\"(ticket_desc)\":\"(.+?)\"/).uniq
         price = price1 + description
         render json: {ticket: price}
       end
     end 

0 个答案:

没有答案