使用Ruby读取json,然后作为json输出

使用Ruby读取json,然后作为json输出,ruby,json,http,file-io,cgi,Ruby,Json,Http,File Io,Cgi,我试图读取从本地.json文件返回的信息。如何让ruby从这个文件返回json信息?到目前为止,我所拥有的: #! /usr/bin/env ruby require 'json' require 'cgi' cgi = CGI::new() if cgi.request_method == 'GET' then json = File.read('path/to/file.json') print json end 如果您使用的是Ruby 1.9.3,则没有文件。请执行以下操作:

我试图读取从本地.json文件返回的信息。如何让ruby从这个文件返回json信息?到目前为止,我所拥有的:

#! /usr/bin/env ruby
require 'json'
require 'cgi'

cgi = CGI::new()

if cgi.request_method == 'GET' then
  json = File.read('path/to/file.json')
  print json
end

如果您使用的是Ruby 1.9.3,则没有文件。请执行以下操作:

if cgi.request_method == 'GET'
    print File.open('path/to/file.json').read
end
你也应该使用


Ruby 1.9.3有
文件。读取
。这是我的建议。
cgi.out("nph"        => true,
    "status"     => "OK",  # == "200 OK"
    "server"     => ENV['SERVER_SOFTWARE'],
    "connection" => "close",
    "type"       => "text/html",
    "charset"    => "iso-2022-jp",
      # Content-Type: text/html; charset=iso-2022-jp
    "language"   => "ja",
    "expires"    => Time.now + (3600 * 24 * 30),
    "cookie"     => [cookie1, cookie2],
    "my_header1" => "my_value",
    "my_header2" => "my_value") { "string" }