【问题标题】:HTTParty double escapes my jsonHTTParty 双重转义我的 json
【发布时间】:2011-11-27 20:55:00
【问题描述】:

我有几个 Ruby 模型需要通过 HTTParty/Put 发送到 Java/RestEasy 服务器。

configuration_mapping.rb:

def as_json(options = {})
  {:configGroup => @group, :coordinates => {@key => @value}}
end

def self.put(endpoint, content, tier = 'nursery')
  response = HTTParty.put(base_uri + endpoint, json_payload(content))
end    

def self.json_payload(content)
  {
    :body => content.to_json,
    :format => :json,
    :headers => {"Content-Type" => "application/json", "content-type" => "application/json", "Accept" => "application/json"}
  }
end

JSON 被双重转义:

{ :body=>" { \"configGroup\":\"测试\", \"坐标\":{ \"集成测试密钥\":\"moo\" } } ", :format=>:json, :headers=>{" Content-Type"=>"application/json", "content-type"
=>“应用程序/json”,“接受”=>“应用程序/json”} }

还有 Jackson JSON 解析器:

2011-11-27 15:34:11,179 错误 [tp-1442358158-0] [报告] [] [asy.core.SynchronousDispatcher] 执行 PUT 失败 /v1/groups/test/mappings;tester=Integration 测试;tier=qa;timeStamp=-4712-01-01 org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException:无法反序列化 [Source: START_OBJECT 标记中的 java.lang.String 实例 org.mortbay.jetty.HttpParser$Input@4092fef5;行:1,列:22] 在

我尝试让 httparty 为我转换为 json,认为 httparty 转义了字符,编写了我自己的 as_json 方法,但这是不是我想要的 json 的输出,错误的字段在这里,我的 as_json 方法不是叫:

{:body=>Config::Client::ConfigurationMapping:0x00000100c78930 @dimensions={"tester"=>"集成测试", "tier"=>"qa", "timeStamp"=>"-4712-01-01"}, @key="集成测试密钥", @group="test", @value="moo">, :format=>:json, :headers=>{"Content-Type"=>"application/json", "content-type"=>"application/json", "Accept"=>"application/json"}}

是什么导致字符串被双重转义?

【问题讨论】:

    标签: ruby ruby-on-rails-3 json httparty


    【解决方案1】:

    我发现了问题,我的 json 格式不正确。我重新格式化了数据包,它工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 2017-01-14
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多