我如何在ruby中解析这个JSON

时间:2017-07-12 15:12:56

标签: ruby ruby-on-rails-4

请帮我在ruby中解析这个json:

"\"{\\\"Details\\\":[{\\\"WaybillNo\\\":\\\"11-17-657\\\",\\\"ShipperName\\\":\\\"Paritosh Singh\\\",\\\"ShipperAddress\\\":\\\"New Delhi, India\\\",\\\"ShipperMob\\\":\\\"0812944\\\",\\\"ConsigneeName\\\":\\\"SANDY\\\",\\\"ConsigneeAddress\\\":\\\"Mumbai India\\\",\\\"ConsigneeMob\\\":\\\"8355485\\\",\\\"Pieces\\\":\\\"1\\\"}]}\""

由于

1 个答案:

答案 0 :(得分:5)

这看起来像JSON中的JSON:

require 'json'

string = "\"{\\\"Details\\\":[{\\\"WaybillNo\\\":\\\"11-17-657\\\",\\\"ShipperName\\\":\\\"Paritosh Singh\\\",\\\"ShipperAddress\\\":\\\"New Delhi, India\\\",\\\"ShipperMob\\\":\\\"0812944\\\",\\\"ConsigneeName\\\":\\\"SANDY\\\",\\\"ConsigneeAddress\\\":\\\"Mumbai India\\\",\\\"ConsigneeMob\\\":\\\"8355485\\\",\\\"Pieces\\\":\\\"1\\\"}]}\""

JSON.parse(JSON.parse(string))
#=> {
#     "Details" => [
#       [0] {
#         "WaybillNo"        => "11-17-657",
#         "ShipperName"      => "Paritosh Singh",
#         "ShipperAddress"   => "New Delhi, India",
#         "ShipperMob"       => "0812944",
#         "ConsigneeName"    => "SANDY",
#         "ConsigneeAddress" => "Mumbai India",
#         "ConsigneeMob"     => "8355485",
#         "Pieces"           => "1"
#       }
#     ]
#   }