【问题标题】:Rails What is the best way to generate a json form a large amout of dataRails 生成大量数据的 json 格式的最佳方法是什么
【发布时间】:2013-12-18 01:24:36
【问题描述】:

我正在开发一个作为移动应用后端的项目。

Una 的 API 调用以 json 形式返回大量数据,生成 json 需要很多时间。

现在,我正在预处理数据以生成包含所有信息的哈希和

Places.each do |item|

      place = item.place

      place.discounts.each do |discount|
        response_item = {
            :id => place.id,
            :latitude => item.latitude,
            :longitude => item.longitude,
            :name => place.name,
            :url_image => place.img,
            :stars => 0,
            :is_habitue => false,#is_habitue,
            :discount => {
                :id => discount.id,
                :title => discount.title,
                :description => discount.description,
                :raw_title => discount.raw_title,
                :expiration => discount.expiration
            }
        }

        categories = []
        place.categories.each do |category|
          categories.append ({
              :name => category.name,
              :label => category.label
          })
        end
        response_item[:categories] = categories

        benefits = []
        discount.benefits.each do |benefit|
          benefits.append ({
            :benefit_type => benefit.benefit_type,
            :label => benefit.label
          })
        end
        response_item[:benefits] = benefits

        processed_places.append response_item
      end
end
render :json => {:places=>processed_places}, :status=>200

我处理 2700 个结果大约需要 1.4 秒,但生成 json 需要超过 6 秒。

谢谢

【问题讨论】:

  • 你是如何处理 JSON 的?更多代码会很棒。
  • @KieranAndrews 我已经更新了这个问题。我添加了如何生成稍后呈现为 ​​json 的哈希

标签: ruby-on-rails json api


【解决方案1】:

也许你可以试试 RABL。它有很棒的功能:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多