1

我正在使用一个 json 解析器,该解析器需要将我的响应包装在对象键哈希中。当我使用 DataMapper 的 .to_json 方法(datamapper/dm-serializer)时,我得到的响应是正确的

get '/plane/all' do
  @plane = Plane.all(:order => :id.desc).to_json(:relationships => {:pilots => {}, :passengers => {}, :cabin => {}})
  @plane
end

JSON 格式的响应

[{"id":2,"name":"Plane 2","picture_url":"https://s3.amazonaws.com","pilots":[{"pilot_id":2,"header":"Bruce Wayne","details":"Bruce loves his batwing that flies at an average speed of 200 mph","picture_url":"www.marvel.com","plane_id":2}],"passengers":[{"passenger_id":2,"name":"Passenger 2","details":"These are the details for passenger 2","picture_url":"www.toobar.com/","plane_id":2}],"cabin":[{"cabin_id":2,"details":"Great Details for has been put in this cabin","picture_url":"www.seatingchart.com","video_link":"www.skyview.com","passenger_passenger_id":2}]},{"id":1,"name":"Plane 1","picture_url":"https://s3.amazonaws.com","pilots":[{"pilot_id":1,"header":"Jack Smith","details":"Jack Smith description","picture_url":"https://www.foobar.com","plane_id":1}],"passengers":[{"passenger_id":1,"name":"Passenger 1","details":"Passenger 1 details","picture_url":"www.toobar.com/","passenger_id":1}],"cabin":[{"cabin_id":1,"details":"Flight details","picture_url":"www.seatingchart.com","video_link":"www.skyview.com","passenger_passenger_id":1}]}]

为了将此响应包装为密钥,我使用了具有自己的 to_json 方法的“json”gem

get '/plane/all' do
  @plane = Plane.all(:order => :id.desc).to_json(:relationships => {:pilots => {}, :passengers => {}, :cabin => {}})
 { "planes" => @plane }.to_json
end

这是回复

{"planes":"[{\"id\":2,\"name\":\"Plane 2\",\"picture_url\":\"https://s3.amazonaws.com\",\"pilots\":[{\"pilot_id\":2,\"header\":\"Bruce Wayne\",\"details\":\"Bruce loves his batwing that flies at an average speed of 200 mph\",\"picture_url\":\"www.marvel.com\",\"plane_id\":2}],\"passengers\":[{\"passenger_id\":2,\"name\":\"Passenger 2\",\"details\":\"These are the details for passenger 2\",\"picture_url\":\"www.toobar.com/\",\"plane_id\":2}],\"cabin\":[{\"cabin_id\":2,\"details\":\"Great Details for has been put in this cabin\",\"picture_url\":\"www.seatingchart.com\",\"video_link\":\"www.skyview.com\",\"passenger_passenger_id\":2}]},{\"id\":1,\"name\":\"Plane 1\",\"picture_url\":\"https://s3.amazonaws.com\",\"pilots\":[{\"pilot_id\":1,\"header\":\"Jack Smith\",\"details\":\"Jack Smith description\",\"picture_url\":\"https://www.foobar.com\",\"plane_id\":1}],\"passengers\":[{\"passenger_id\":1,\"name\":\"Passenger 1\",\"details\":\"Passenger 1 details\",\"picture_url\":\"www.toobar.com/\",\"plane_id\":1}],\"cabin\":[{\"cabin_id\":1,\"details\":\"Flight details\",\"picture_url\":\"www.seatingchart.com\",\"video_link\":\"www.skyview.com\",\"passenger_passenger_id\":1}]}]"}

长话短说,我得到的 JSON 响应中有反斜杠,而我使用的 JSON 解析器表明它只是一个对象,而不是我之前拥有的两个对象。

任何可以帮助我的人,将不胜感激。

4

1 回答 1

0

这是我的解决方法。如果没有视图,则无需使用实例变量。

def '/plane/all' do
    planes = Plane.all(:order => :id.desc).to_json(:relationships => {:pilots => {}, :passengers => {}, :cabin => {}})
    { "planes" => JSON.parse(planes) }.to_json
end
于 2019-03-13T18:13:57.990 回答