Ruby:JSON.parse返回未定义的方法`bytesize'

don*_*ald 7 ruby json sinatra

response = Typhoeus::Request.get("http://localhost:3000/api/api_email/#{@api_id}.json")
    JSON.parse(response.body)
Run Code Online (Sandbox Code Playgroud)

响应是一个JSON对象,但在尝试解析它时出错.

undefined method `bytesize' for 
Run Code Online (Sandbox Code Playgroud)

我想访问JSON对象.

错误:

NoMethodError at /api/v1/a71040739d6cc50e89aff56601af67/2011-10-1
undefined method `bytesize' for {"xpto"=>{"email

"=>"test@gmail.com"}}:Hash

file: utils.rb location: bytesize line: 239
Run Code Online (Sandbox Code Playgroud)

回溯:

/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/httpserver.rb in service
      si.service(req, res)
/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/httpserver.rb in run
          server.service(req, res)
/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/server.rb in block in start_thread
          block ? block.call(sock) : run(sock)
Run Code Online (Sandbox Code Playgroud)

这是如何生成的:

 @api_id = params[:api_id]
 @bucket = Bucket.where(:api => @api_id)
 respond_with(@bucket, :only => [:email])
Run Code Online (Sandbox Code Playgroud)

返回的.json文件包含:

[{"xpto":{"email":"test@gmail.com"}}]
Run Code Online (Sandbox Code Playgroud)

Rom*_*man 6

这很奇怪,因为似乎response.body已经是哈希了!(即解析的JSON字符串),或者,您可能在webrick的日志中看到了这一点,因此问题在于生成 JSON响应,而不是解析它.回溯没有意义:(