HTTParty 双重转义我的 json
我有一些 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"
=>“application/json”,“接受”=>“application/json”} }
和 Jackson JSON 解析器 borks:
2011-11-27 15:34:11,179 错误 [tp-1442358158-0] [报告] [] [asy.core.SynchronousDispatcher] 执行 PUT 失败 /v1/groups/test/mappings;tester=集成 测试;层=qa;时间戳=-4712-01-01 org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException:无法反序列化 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", “内容类型”=>“application/json”,“接受”=>“application/json”}}
是什么导致字符串变成双重转义?
I have a few models in Ruby that I need to send to a Java/RestEasy server via HTTParty/Put.
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
The JSON gets double escaped:
{ :body=>" {
\"configGroup\":\"test\",
\"coordinates\":{
\"Integration Test Key\":\"moo\"
} } ", :format=>:json, :headers=>{" Content-Type"=>"application/json", "content-type"
=>"application/json", "Accept" =>"application/json" } }
And Jackson JSON parser borks:
2011-11-27 15:34:11,179 ERROR [tp-1442358158-0] [REPORT] []
[asy.core.SynchronousDispatcher] Failed executing PUT
/v1/groups/test/mappings;tester=Integration
Test;tier=qa;timeStamp=-4712-01-01
org.jboss.resteasy.spi.ReaderException:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize
instance of java.lang.String out of START_OBJECT token at [Source:
org.mortbay.jetty.HttpParser$Input@4092fef5; line: 1, column: 22] at
I tried letting httparty convert to json for me, thinking httparty escaped the characters, writing my own as_json method, but this is the output which is not the json I wanted, the wrong fields are in here and my as_json method is not called:
{:body=>Config::Client::ConfigurationMapping:0x00000100c78930
@dimensions={"tester"=>"Integration Test", "tier"=>"qa",
"timeStamp"=>"-4712-01-01"}, @key="Integration Test Key",
@group="test", @value="moo">, :format=>:json,
:headers=>{"Content-Type"=>"application/json",
"content-type"=>"application/json", "Accept"=>"application/json"}}
What is causing the string to become double escaped?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现问题所在了,我的 json 格式不正确。我重新格式化了数据包并且它起作用了。
I figured out the problem, my json wasn't formatted correctly. I reformatted the packet and it worked.