我正在研究一个示例 Ruby/Grape 示例,除了 json 被转义外,一切正常。我也是全新的 ruby 及其框架(仅 3 天),如果这个问题是补救性的并且提前谢谢你,我很抱歉
我相当确定不应该转义引号,无论如何这是转义的输出:
"{\"word\":\"test\",\"sentiment\":\"unkown\"}"
我的代码
require 'rubygems'
require 'grape'
require 'json'
class SentimentApiV1 < Grape::API
version 'v1', :using => :path, :vendor => '3scale'
format :json
resource :words do
get ':word' do
{:word => params[:word], :sentiment => "unkown"}.to_json
end
post ':word' do
{:word => params[:word], :result => "thinking"}.to_json
end
end
resource :sentences do
get ':sentence' do
{:sentence => params[:sentence], :result => "unkown"}.to_json
end
end
end
配置.ru
$:.unshift "./app"
require 'sentimentapi_v1.rb'
run SentimentApiV1
包和版本
C:\Ruby-Projects\GrapeTest>bundle install
Using i18n (0.6.4)
Using minitest (4.7.5)
Using multi_json (1.7.7)
Using atomic (1.1.10)
Using thread_safe (0.1.0)
Using tzinfo (0.3.37)
Using activesupport (4.0.0)
Using backports (3.3.3)
Using builder (3.2.2)
Using daemons (1.1.9)
Using descendants_tracker (0.0.1)
Using hashie (2.0.5)
Using multi_xml (0.5.4)
Using rack (1.5.2)
Using rack-accept (0.4.5)
Using rack-mount (0.8.3)
Using virtus (0.5.5)
Using grape (0.5.0)
Using json (1.8.0)
Using thin (1.5.1)
Using bundler (1.3.5)
我正在运行 ruby 2.0、grape .5、windows 8 64 位
最佳答案
发生转义的原因是你不需要在最后调用 #to_json
因为在第 7 行你指定了 format :json
作为输出格式。
关于 ruby 葡萄 : json output is escaped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756372/