首页 文章

如何在Ruby on Rails中“漂亮”格式化我的JSON输出?

提问于
浏览
544

我希望Ruby on Rails中的JSON输出“漂亮”或格式良好 .

现在,我打电话给 to_json ,我的JSON全部在一条线上 . 有时,如果JSON输出流中存在问题,则很难看到 .

有没有办法配置或方法使我的JSON在Rails中“漂亮”或格式良好?

16 回答

  • 6

    感谢Rack Middleware和Rails 3,您可以为每个请求输出漂亮的JSON,而无需更改应用程序的任何控制器 . 我已经写了这样的中间件片段,我在浏览器和 curl 输出中得到了很好的打印JSON .

    class PrettyJsonResponse
      def initialize(app)
        @app = app
      end
    
      def call(env)
        status, headers, response = @app.call(env)
        if headers["Content-Type"] =~ /^application\/json/
          obj = JSON.parse(response.body)
          pretty_str = JSON.pretty_unparse(obj)
          response = [pretty_str]
          headers["Content-Length"] = pretty_str.bytesize.to_s
        end
        [status, headers, response]
      end
    end
    

    上面的代码应该放在你的Rails项目的 app/middleware/pretty_json_response.rb 中 . 最后一步是在 config/environments/development.rb 中注册中间件:

    config.middleware.use PrettyJsonResponse
    

    I don't recommend to use it in production.rb . JSON重新分析可能会降低 生产环境 应用程序的响应时间和吞吐量 . 最终可能会引入额外的逻辑,例如'X-Pretty-Json: true'标头,以按需触发手动卷曲请求的格式化 .

    (使用Rails 3.2.8-5.0.0,Ruby 1.9.3-2.2.0,Linux测试)

  • 20

    将ActiveRecord对象转储到JSON(在Rails控制台中):

    pp User.first.as_json
    
    # => {
     "id" => 1,
     "first_name" => "Polar",
     "last_name" => "Bear"
    }
    
  • 61

    看看awesome_print . 将JSON字符串解析为Ruby Hash,然后使用awesome_print显示它,如下所示:

    require "awesome_print"
    require "json"
    
    json = '{"holy": ["nested", "json"], "batman!": {"a": 1, "b": 2}}'
    
    ap(JSON.parse(json))
    

    有了上述内容,您将看到:

    {
      "holy" => [
        [0] "nested",
        [1] "json"
      ],
      "batman!" => {
        "a" => 1,
        "b" => 2
      }
    }
    

    awesome_print还会添加一些Stack Overflow不会显示的颜色:)

  • 1

    我使用以下内容,因为我发现 Headers ,状态和JSON输出有用作一个集合 . 呼叫例程根据railscasts演示文稿的推荐进行分析:http://railscasts.com/episodes/151-rack-middleware?autoplay=true

    class LogJson
    
      def initialize(app)
        @app = app
      end
    
      def call(env)
        dup._call(env)
      end
    
      def _call(env)
        @status, @headers, @response = @app.call(env)
        [@status, @headers, self]
      end
    
      def each(&block)
        if @headers["Content-Type"] =~ /^application\/json/
          obj = JSON.parse(@response.body)
          pretty_str = JSON.pretty_unparse(obj)
          @headers["Content-Length"] = Rack::Utils.bytesize(pretty_str).to_s
          Rails.logger.info ("HTTP Headers:  #{ @headers } ")
          Rails.logger.info ("HTTP Status:  #{ @status } ")
          Rails.logger.info ("JSON Response:  #{ pretty_str} ")
        end
    
        @response.each(&block)
      end
      end
    
  • 10

    如果你(像我一样)发现Ruby的JSON库中内置的 pretty_generate 选项不够"pretty",我建议使用我自己的NeatJSON gem进行格式化 .

    要使用它 gem install neatjson 然后使用 JSON.neat_generate 而不是 JSON.pretty_generate .

    就像Ruby的 pp 一样,当它们适合时它会将对象和数组保持在一条线上,但是根据需要包装到多个 . 例如:

    {
      "navigation.createroute.poi":[
        {"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
        {"text":"Take me to the airport","params":{"poi":"airport"}},
        {"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
        {"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
        {"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
        {
          "text":"Go to the Hilton by the Airport",
          "params":{"poi":"Hilton","location":"Airport"}
        },
        {
          "text":"Take me to the Fry's in Fresno",
          "params":{"poi":"Fry's","location":"Fresno"}
        }
      ],
      "navigation.eta":[
        {"text":"When will we get there?"},
        {"text":"When will I arrive?"},
        {"text":"What time will I get to the destination?"},
        {"text":"What time will I reach the destination?"},
        {"text":"What time will it be when I arrive?"}
      ]
    }
    

    它还支持各种formatting options以进一步自定义您的输出 . 例如,冒号之前/之后有多少个空格?逗号之前/之后?在数组和对象的括号内?你想对对象的键进行排序吗?你想要将所有冒号排成一列吗?

  • 4

    HTML中的 <pre> 标记与 JSON.pretty_generate 一起使用,将在您的视图中呈现JSON . 当我的杰出老板告诉我这件事时,我很高兴:

    <% if !@data.blank? %>
       <pre><%= JSON.pretty_generate(@data) %></pre>
    <% end %>
    
  • 2
    #At Controller
    def branch
        @data = Model.all
        render json: JSON.pretty_generate(@data.as_json)
    end
    
  • 868

    使用 pretty_generate() 函数,该函数内置于更高版本的JSON中 . 例如:

    require 'json'
    my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
    puts JSON.pretty_generate(my_object)
    

    哪个让你:

    {
      "array": [
        1,
        2,
        3,
        {
          "sample": "hash"
        }
      ],
      "foo": "bar"
    }
    
  • 1

    使用 <pre> html代码和 pretty_generate 是一个好方法:

    <%
      require 'json'
    
      hash = JSON[{hey: "test", num: [{one: 1, two: 2, threes: [{three: 3, tthree: 33}]}]}.to_json] 
    %>
    
    <pre>
      <%=  JSON.pretty_generate(hash) %>
    </pre>
    
  • 10

    这是从this excellent answer by @gertas修改的中间件解决方案 . 此解决方案不是Rails特定的 - 它应该适用于任何Rack应用程序 .

    这里使用的中间件技术,使用#each,由Eifion Bedford在ASCIIcasts 151: Rack Middleware解释 .

    此代码位于app / middleware / pretty_json_response.rb中:

    class PrettyJsonResponse
    
      def initialize(app)
        @app = app
      end
    
      def call(env)
        @status, @headers, @response = @app.call(env)
        [@status, @headers, self]
      end
    
      def each(&block)
        @response.each do |body|
          if @headers["Content-Type"] =~ /^application\/json/
            body = pretty_print(body)
          end
          block.call(body)
        end
      end
    
      private
    
      def pretty_print(json)
        obj = JSON.parse(json)  
        JSON.pretty_unparse(obj)
      end
    
    end
    

    要打开它,请将其添加到config / environments / test.rb和config / environments / development.rb:

    config.middleware.use "PrettyJsonResponse"
    

    正如@gertas在他的这个解决方案版本中警告的那样,避免在 生产环境 中使用它 . 这有点慢 .

    用Rails 4.1.6测试 .

  • 2

    我使用了宝石CodeRay,效果很好 . 格式包括颜色,它识别许多不同的格式 .

    我在一个可以用于调试rails API的gem上使用它,它运行得很好 .

    顺便说一句,宝石名为'api_explorer'(http://www.github.com/toptierlabs/api_explorer

  • 1

    如果您希望在Rails控制器操作中快速实现此操作以发送JSON响应:

    def index
      my_json = '{ "key": "value" }'
      render json: JSON.pretty_generate( JSON.parse my_json )
    end
    
  • 10

    这是我在自己的搜索过程中从其他帖子中获得的解决方案 .

    这允许您根据需要将pp和jj输出发送到文件 .

    require "pp"
    require "json"
    
    class File
      def pp(*objs)
        objs.each {|obj|
          PP.pp(obj, self)
        }
        objs.size <= 1 ? objs.first : objs
      end
      def jj(*objs)
        objs.each {|obj|
          obj = JSON.parse(obj.to_json)
          self.puts JSON.pretty_generate(obj)
        }
        objs.size <= 1 ? objs.first : objs
      end
    end
    
    test_object = { :name => { first: "Christopher", last: "Mullins" }, :grades => [ "English" => "B+", "Algebra" => "A+" ] }
    
    test_json_object = JSON.parse(test_object.to_json)
    
    File.open("log/object_dump.txt", "w") do |file|
      file.pp(test_object)
    end
    
    File.open("log/json_dump.txt", "w") do |file|
      file.jj(test_json_object)
    end
    
  • 68

    如果您正在使用RABL,则可以按here所述配置它以使用JSON.pretty_generate:

    class PrettyJson
      def self.dump(object)
        JSON.pretty_generate(object, {:indent => "  "})
      end
    end
    
    Rabl.configure do |config|
      ...
      config.json_engine = PrettyJson if Rails.env.development?
      ...
    end
    

    使用JSON.pretty_generate的一个问题是JSON模式验证器将不再满足于您的日期时间字符串 . 您可以使用以下命令修复config / initializers / rabl_config.rb中的那些:

    ActiveSupport::TimeWithZone.class_eval do
      alias_method :orig_to_s, :to_s
      def to_s(format = :default)
        format == :default ? iso8601 : orig_to_s(format)
      end
    end
    
  • 9

    如果你想:

    • 自动从您的应用程序中排除所有传出的JSON响应 .

    • 避免污染Object#to_json / #as_json

    • 避免使用中间件解析/重新呈现JSON(YUCK!)

    • 用RAILS方式做吧!

    然后......替换JSON的ActionController :: Renderer!只需将以下代码添加到ApplicationController:

    ActionController::Renderers.add :json do |json, options|
      unless json.kind_of?(String)
        json = json.as_json(options) if json.respond_to?(:as_json)
        json = JSON.pretty_generate(json, options)
      end
    
      if options[:callback].present?
        self.content_type ||= Mime::JS
        "#{options[:callback]}(#{json})"
      else
        self.content_type ||= Mime::JSON
        json
      end
    end
    
  • 2
    # example of use:
    a_hash = {user_info: {type: "query_service", e_mail: "my@email.com", phone: "+79876543322"}, cars_makers: ["bmw", "mitsubishi"], car_models: [bmw: {model: "1er", year_mfc: 2006}, mitsubishi: {model: "pajero", year_mfc: 1997}]}
    pretty_html = a_hash.pretty_html
    
    # include this module to your libs:
    module MyPrettyPrint
        def pretty_html indent = 0
            result = ""
            if self.class == Hash
                self.each do |key, value|
                    result += "#{key} : #{[Array, Hash].include?(value.class) ? value.pretty_html(indent+1) : value} "
                end
            elsif self.class == Array
                result = "[#{self.join(', ')}]"
            end
            "#{result}"
        end
    
    end
    
    class Hash
        include MyPrettyPrint
    end
    
    class Array
        include MyPrettyPrint
    end
    

相关问题