我正在从 Rails 控制台访问我的 Rails 操作之一:
> @consumer = OAuth::Consumer.new(
> x,
> x,
> site: "http://localhost:3000"
> )
> request = @consumer.create_signed_request(:get,"/get_user_info?email=x")
> uri = URI.parse("http://localhost:3000")
> http = Net::HTTP.new(uri.host, uri.port)
> http.request(request).body
=> "{\"first_thing\":\"Nick\",\"second_thing\":\"2012-12-26T11:41:11Z\",\"third_thing\":\"2012-12-26T11:40:03Z\"}"
> http.request(request).body.class
=> String
该操作应该返回 JSON 格式的哈希值,而不是字符串。以下是该操作的结束方式:
render json: {
first_thing: x,
second_thing: x,
third_thing: x
}
为什么这会以字符串形式出现?我使用的是 Rails 3.2.0 和 Ruby 1.9.3。
最佳答案
您始终会从 HTTP 请求中获取字符串。 render :json
只是将哈希值转换为 JSON 字符串。
您需要对字符串执行JSON.parse
。
关于ruby-on-rails - 为什么这个 Rails 操作返回字符串而不是 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14040615/