Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Grape:json输出被转义_Ruby_Grape_Grape Api - Fatal编程技术网

Ruby Grape:json输出被转义

Ruby Grape:json输出被转义,ruby,grape,grape-api,Ruby,Grape,Grape Api,我正在编写一个示例Ruby/Grape,除了json外,其他一切都正常。我也是全新的ruby及其框架(仅3天),如果这个问题是补救性的,我很抱歉,并提前向您表示感谢 我相当肯定引号不应该转义,无论如何,转义的输出如下: "{\"word\":\"test\",\"sentiment\":\"unkown\"}" 我的代码 require 'rubygems' require 'grape' require 'json' class SentimentApiV1 < Grape::AP

我正在编写一个示例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

我正在运行ruby 2.0、grape.5、windows 8 64位

很好-显然,最后不需要to_json。也许是双重逃跑之类的。演示中肯定有to_json,就是这样

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"}
    end

    post ':word' do
      {:word => params[:word], :result => "thinking"}
    end 
  end

  resource :sentences do
    get ':sentence' do
      {:sentence => params[:sentence], :result => "unkown"}
    end
  end

end
需要“rubygems”
需要“葡萄”
需要“json”
类apiv1:path,供应商=>'3scale'
格式:json
资料来源:单词可以
获取“:单词”do
{:word=>params[:word],:touction=>“unknown”}
终止
贴子:单词“do”
{:word=>params[:word],:result=>thinking}
终止
终止
资源:句子做什么
得到:句子“做”
{:句子=>参数[:句子],:结果=>“未知”}
终止
终止
终止

很好-显然,最后不需要to_json。也许是双重逃跑之类的。演示中肯定有to_json,就是这样

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"}
    end

    post ':word' do
      {:word => params[:word], :result => "thinking"}
    end 
  end

  resource :sentences do
    get ':sentence' do
      {:sentence => params[:sentence], :result => "unkown"}
    end
  end

end
需要“rubygems”
需要“葡萄”
需要“json”
类apiv1:path,供应商=>'3scale'
格式:json
资料来源:单词可以
获取“:单词”do
{:word=>params[:word],:touction=>“unknown”}
终止
贴子:单词“do”
{:word=>params[:word],:result=>thinking}
终止
终止
资源:句子做什么
得到:句子“做”
{:句子=>参数[:句子],:结果=>“未知”}
终止
终止
终止

发生转义的原因是因为在第7行指定了
格式:json
作为输出格式,所以不需要在结尾调用
\to#json

发生转义的原因是因为在第7行指定了
\to#json
而不需要在结尾调用
format:json
作为输出格式。

您的结果,
“{\'word\':\'test\',\'emotional\':\'unknown\'}”
实际上是有效的json。这是字符串
{“单词”:“测试”,“情感”:“未知”}
。通过调用
到_json
,您已经将哈希转换为字符串,然后Grape将返回您给它的内容。使用
作为\u json
,它将返回一个散列,Grape将负责正确地序列化它。

您的结果,
“{\'word\”:\“test\”,“touction\”:\“unknown\”}
实际上是有效的json。这是字符串
{“单词”:“测试”,“情感”:“未知”}
。通过调用
到_json
,您已经将哈希转换为字符串,然后Grape将返回您给它的内容。改用
作为_json
,它将返回一个散列,Grape将负责正确地序列化它