【问题标题】:How can I stop Grape from JSON-encoding my output twice?如何阻止 Grape 对我的输出进行两次 JSON 编码?
【发布时间】:2014-09-03 11:18:42
【问题描述】:

我目前正在实现一个基于 JSON 的 Grape API。我已经为 JSON 序列化和反序列化集成了 roar。执行以下代码时,似乎 JSON 被编码了两次,导致响应带有斜杠。 Grape 是否有必要在需要时抑制 JSON 解码?

json = UserRepresenter.new(user).to_json { status: StatusResponse::VALID, message: json }

【问题讨论】:

    标签: json ruby-on-rails-4 grape rails-roar


    【解决方案1】:

    Grape 自动将输出编码为 JSON(当指定 format :json 时),因此代码中的 to_json 调用是多余的。

    尝试将您的代码更改为:

    { status: StatusResponse::VALID, message: UserRepresenter.new(user) }
    

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多