我有一些 Ruby 模型需要通过 HTTParty/Put 发送到 Java/RestEasy 服务器。
配置映射.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\":\"test\", \"坐标\":{ \"集成测试密钥\":\"moo\" } } ", :format=>:json, :headers=>{" Content-Type"=>"application/json", "content-type"
=>"application/json", "Accept" =>"application/json" } }
和杰克逊 JSON 解析器 borks:
2011-11-27 15:34:11,179 错误 [tp-1442358158-0] [报告] [] [asy.core.SynchronousDispatcher] 执行 PUT /v1/groups/test/mappings 失败;tester=集成测试;tier=qa ;timeStamp=-4712-01-01 org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: 无法从 [来源: org.mortbay 的 START_OBJECT 令牌中反序列化 java.lang.String 的实例.jetty.HttpParser$Input@4092fef5; 行:1,列:22] 在
我尝试让 httparty 为我转换为 json,认为 httparty 转义了字符,编写了我自己的 as_json 方法,但这是不是我想要的 json 的输出,错误的字段在这里并且我的 as_json 方法没有被调用:
{:body=>Config::Client::ConfigurationMapping:0x00000100c78930 @dimensions={"tester"=>"Integration Test", "tier"=>"qa", "timeStamp"=>"-4712-01-01" }, @key="集成测试密钥", @group="test", @value="moo">, :format=>:json, :headers=>{"Content-Type"=>"application/json" , "内容类型"=>"应用程序/json", "接受"=>"应用程序/json"}}
是什么导致字符串被双重转义?