将json存储在数据库中,稍后再解析它

时间:2011-06-08 12:21:46

标签: ruby-on-rails json storage

{"meta":{"code":200},"response":{"groups":[{"type":"places","name":"Matching Places","items":[{"id":"4c76542e6f789c7467ec514c","name":"india gate","contact":{},"location":{"address":"New Delhi, India","crossStreet":"NOne","city":"delhi","state":"delhi","postalCode":"india","lat":28.635308,"lng":77.22496,"distance":0},"categories":[],"verified":false,"stats":{"checkinsCount":23,"usersCount":17},"hereNow":{"count":0}},{"id":"4b5eeab3f964a520ca9d29e3","name":"India gate","contact":{},"location":{"address":"August Kranti Maidan","crossStreet":"Rajpath","city":"New Delhi","state":"Delhi","lat":28.61300733365442,"lng":77.2295093536377,"distance":2521},"categories":[{"id":"4bf58dd8d48988d166941735","name":"Sculpture Garden","pluralName":"Sculpture Gardens","icon":"https://foursquare.com/img/categories/parks_outdoors/default.png","parents":["Great Outdoors"],"primary":true}],"verified":false,"stats":{"checkinsCount":469,"usersCount":295},"hereNow":{"count":0}},{"id":"4c5962127b04952174db881f","name":"Barista Lavazza, New Delhi India","contact":{},"location":{"address":"New Delhi","city":"New Delhi","state":"India","lat":28.635308,"lng":77.22496,"distance":0},"categories":[],"verified":true,"stats":{"checkinsCount":5,"usersCount":5},"hereNow":{"count":0}},{"id":"4b987a2ff964a5200d4435e3","name":"India International Center","contact":{"phone":"01124619431"},"location":{"address":"40, Lodi Road, Max Mueller Marg,","city":"New Delhi","state":"Delhi","postalCode":"110001","lat":28.59266125413463,"lng":77.22272872924805,"distance":4752},"categories":[{"id":"4bf58dd8d48988d1ff931735","name":"Convention Center","pluralName":"Convention Centers","icon":"https://foursquare.com/img/categories/building/default.png","parents":["Homes, Work, Others"],"primary":true}],"verified":false,"stats":{"checkinsCount":54,"usersCount":41},"hereNow":{"count":0}},{"id":"4c47ebff417b20a1f052dfa9","name":"Times of India - ITO","contact":{},"location":{"city":"New Delhi","state":"Delhi","postalCode":"110002","lat":28.634102,"lng":77.240443,"distance":1518},"categories":[{"id":"4bf58dd8d48988d124941735","name":"Office","pluralName":"Offices","icon":"https://foursquare.com/img/categories/building/default.png","parents":["Homes, Work, Others"],"primary":true}],"verified":false,"stats":{"checkinsCount":50,"usersCount":22},"hereNow":{"count":0}},{"id":"4cd57bbe7da9a35d38cee9b9","name":"Legends of India","contact":{},"location":{"address":"Connaught Place","city":"New Delhi","state":"Delhi","postalCode":"110001","lat":28.6288555,"lng":77.2263342,"distance":730},"categories":[{"id":"4bf58dd8d48988d10f941735","name":"Indian Restaurant","pluralName":"Indian Restaurants","icon":"https://foursquare.com/img/categories/food/indian.png","parents":["Food"],"primary":true}],"verified":false,"stats":{"checkinsCount":9,"usersCount":8},"hereNow":{"count":0}},{"id":"4c794984794e224b8fe16428","name":"Club India","contact":{},"location":{"address":"Pahar Ganj","city":"Delhi","state":"Delhi","lat":28.6416037,"lng":77.2132106,"distance":1344},"categories":[{"id":"4bf58dd8d48988d10b941735","name":"Falafel Restaurant","pluralName":"Falafel Restaurants","icon":"https://foursquare.com/img/categories/food/default.png","parents":["Food"],"primary":true},{"id":"4bf58dd8d48988d10f941735","name":"Indian Restaurant","pluralName":"Indian Restaurants","icon":"https://foursquare.com/img/categories/food/indian.png","parents":["Food"]}],"verified":false,"stats":{"checkinsCount":27,"usersCount":21},"hereNow":{"count":0}},{"id":"4c4148b6cc410f479e12ab61","name":"Fab india","contact":{},"location":{"address":"Vasant kunj market","city":"New Delhi","state":"Delhi","lat":28.635308,"lng":77.22496,"distance":0},"categories":[],"verified":false,"stats":{"checkinsCount":3,"usersCount":2},"hereNow":{"count":0}},{"id":"4bb6f9df941ad13a313320e3","name":"India Habitat Center","contact":{},"location":{"address":"Lodhi Road","city":"New Delhi","state":"India","postalCode":"110003","lat":28.6099397,"lng":77.2322962,"distance":2913},"categories":[{"id":"4bf58dd8d48988d174941735","name":"Coworking Space","pluralName":"Coworking Spaces","icon":"https://foursquare.com/img/categories/building/default.png","parents":["Homes, Work, Others","Offices"],"primary":true}],"verified":false,"stats":{"checkinsCount":40,"usersCount":35},"hereNow":{"count":0}},{"id":"4c347c9a213c2d7faf81385d","name":"Indian Coffee House","contact":{},"location":{"city":"New Delhi","state":"Delhi","lat":28.630334,"lng":77.216895,"distance":963},"categories":[{"id":"4bf58dd8d48988d16d941735","name":"Café","pluralName":"Cafés","icon":"https://foursquare.com/img/categories/food/cafe.png","parents":["Food"],"primary":true}],"verified":false,"stats":{"checkinsCount":8,"usersCount":8},"hereNow":{"count":0}},{"id":"4c18b04bd4d9c92805b9ef29","name":"Office India","contact":{},"location":{"address":"Delhi","city":"New Delhi","state":"Delhi","lat":28.635308,"lng":77.22496,"distance":0},"categories":[{"id":"4bf58dd8d48988d1e1931735","name":"Arcade","pluralName":"Arcades","icon":"https://foursquare.com/img/categories/arts_entertainment/arcade.png","parents":["Arts & Entertainment"],"primary":true}],"verified":false,"stats":{"checkinsCount":3,"usersCount":3},"hereNow":{"count":0}},{"id":"4c6fb485d7fab1f765245cc9","name":"Supreme Court of India","contact":{},"location":{"address":"Bhagwan Dass Road","city":"New Delhi","state":"India","postalCode":"110001","lat":28.622321885531807,"lng":77.23936915397644,"distance":2017},"categories":[{"id":"4bf58dd8d48988d12b941735","name":"Courthouse","pluralName":"Courthouses","icon":"https://foursquare.com/img/categories/building/government_courthouse.png","parents":["Homes, Work, Others","Government Buildings"],"primary":true}],"verified":false,"stats":{"checkinsCount":56,"usersCount":36},"hereNow":{"count":0}},{"id":"4c1b519bb306c928d2f061b7","name":"Microsoft India","contact":{},"location":{"address":"Eros Corporate Tower","crossStreet":"Nehru Place","city":"New Delhi","state":"Delhi","lat":28.635308,"lng":77.22496,"distance":0},"categories":[],"verified":false,"stats":{"checkinsCount":2,"usersCount":2},"hereNow":{"count":0}},{"id":"4ce774989f776ea884425822","name":"India International Trade Fair 2010","contact":{},"location":{"address":"Pragati maidan","city":"Delhi","lat":28.61795614,"lng":77.2456002,"distance":2792},"categories":[{"id":"4bf58dd8d48988d182941735","name":"Theme Park","pluralName":"Theme Parks","icon":"https://foursquare.com/img/categories/arts_entertainment/themepark.png","parents":["Arts & Entertainment"],"primary":true}],"verified":false,"stats":{"checkinsCount":20,"usersCount":20},"hereNow":{"count":0}},{"id":"4c62ae1e58810f47c5b7071e","name":"AT Express India Limited","contact":{"phone":"1800115588","twitter":"atexpress"},"location":{"address":"Kailash Building","crossStreet":"Connaught Place","city":"New Delhi","state":"Delhi","postalCode":"110001","country":"India","lat":28.629582707216333,"lng":77.2217845916748,"distance":708},"categories":[],"verified":false,"stats":{"checkinsCount":7,"usersCount":1},"hereNow":{"count":0}},{"id":"4c47ec7a1ddec928b6f99e32","name":"Indian Express ITO","contact":{},"location":{"city":"New Delhi","state":"Delhi","postalCode":"110002","lat":28.634102,"lng":77.240443,"distance":1518},"categories":[{"id":"4bf58dd8d48988d124941735","name":"Office","pluralName":"Offices","icon":"https://foursquare.com/img/categories/building/default.png","parents":["Homes, Work, Others"],"primary":true}],"verified":false,"stats":{"checkinsCount":16,"usersCount":4},"hereNow":{"count":0}},{"id":"4d35a8ac98336dcbf89d43f0","name":"India Gate","contact":{},"location":{"lat":28.61363928,"lng":77.218888,"distance":2484},"categories":[{"id":"4bf58dd8d48988d162941735","name":"Other Great Outdoors","pluralName":"Other Great Outdoors","icon":"https://foursquare.com/img/categories/parks_outdoors/default.png","parents":["Great Outdoors"],"primary":true}],"verified":false,"stats":{"checkinsCount":32,"usersCount":27},"hereNow":{"count":0}},{"id":"4bee9223ada6b713b6372206","name":"national sports club of india","contact":{},"location":{"address":"Pragati Maidan","crossStreet":"Purana quila Road","city":"Delhi","state":"New Delhi","lat":28.616813,"lng":77.243359,"distance":2733},"categories":[{"id":"4bf58dd8d48988d15e941735","name":"Pool","pluralName":"Pools","icon":"https://foursquare.com/img/categories/parks_outdoors/pool.png","parents":["Great Outdoors"],"primary":true}],"verified":false,"stats":{"checkinsCount":16,"usersCount":11},"hereNow":{"count":0}},{"id":"4d4d658b13d88cfa9b7a08da","name":"Parikrama, Connaught Place, Delhi, India","contact":{},"location":{"address":"Parikrama, Connaught Place, Delhi, India","city":"New Delhi","state":"Delhi","lat":28.628906,"lng":77.2230523,"distance":736},"categories":[{"id":"4bf58dd8d48988d10f941735","name":"Indian Restaurant","pluralName":"Indian Restaurants","icon":"https://foursquare.com/img/categories/food/indian.png","parents":["Food"],"primary":true}],"verified":false,"stats":{"checkinsCount":3,"usersCount":3},"hereNow":{"count":0}},{"id":"4c8c74beb118b713196b1a01","name":"Indiapetstore collection center","contact":{},"location":{"address":"Shop no 6, P-40","crossStreet":"Opposit fine home appt.","city":"Pandav nagar, mayur vihar 1","state":"Delhi","lat":28.635308,"lng":77.22496,"distance":0},"categories":[],"verified":false,"stats":{"checkinsCount":1,"usersCount":1},"hereNow":{"count":0}},{"id":"4da449beb521224b8bc52eee","name":"Godfrey Phillips India","contact":{},"location":{"address":"Cp","city":"New delhi","lat":28.6306585,"lng":77.2175052,"distance":893},"categories":[],"verified":false,"stats":{"checkinsCount":3,"usersCount":1},"hereNow":{"count":0}},{"id":"4da0e2478c8d5941d63a3b0f","name":"INDIA Gate","contact":{},"location":{"address":"Central Delhi, New Delhi, Delhi 110001, India","city":"New Delhi","state":"Delhi","postalCode":"110001","lat":28.6150668,"lng":77.2117789,"distance":2595},"categories":[{"id":"4bf58dd8d48988d1f6931735","name":"General Travel","pluralName":"General Travel","icon":"https://foursquare.com/img/categories/travel/default.png","parents":["Travel Spots"],"primary":true}],"verified":false,"stats":{"checkinsCount":15,"usersCount":9},"hereNow":{"count":0}},{"id":"4c48838e417b20a12831e1a9","name":"India gate lovely chuski","contact":{},"location":{"city":"New Delhi","state":"Delhi","lat":28.60824,"lng":77.220564,"distance":3043},"categories":[],"verified":false,"stats":{"checkinsCount":8,"usersCount":8},"hereNow":{"count":0}},{"id":"4d6379dad6b26ea8c5b54f7a","name":"Food Corporation Of India","contact":{},"location":{"lat":28.631067,"lng":77.227235,"distance":521},"categories":[{"id":"4bf58dd8d48988d125941735","name":"Tech Startup","pluralName":"Tech Startups","icon":"https://foursquare.com/img/categories/building/default.png","parents":["Homes, Work, Others","Offices"],"primary":true}],"verified":false,"stats":{"checkinsCount":2,"usersCount":1},"hereNow":{"count":0}},{"id":"4c5413a306901b8dc8f22f4b","name":"Press Club Of India","contact":{},"location":{"crossStreet":"Raisina road","city":"New Delhi","state":"Delhi","lat":28.618901,"lng":77.214178,"distance":2108},"categories":[{"id":"4bf58dd8d48988d10f941735","name":"Indian Restaurant","pluralName":"Indian Restaurants","icon":"https://foursquare.com/img/categories/food/indian.png","parents":["Food"],"primary":true}],"verified":false,"stats":{"checkinsCount":18,"usersCount":16},"hereNow":{"count":0}},{"id":"4d950ca0d1bca09007ee4bb4","name":"new delhi, india","contact":{},"location":{"address":"new delhi, delhi","city":"delhi","state":"delhi","lat":28.635308,"lng":77.22496,"distance":0},"categories":[],"verified":false,"stats":{"checkinsCount":1,"usersCount":1},"hereNow":{"count":0}},{"id":"4bc45d1074a9a593afabd5f6","name":"DEF India office","contact":{"twitter":"defindia"},"location":{"address":"3rd floor , 44 kalu sarai , Near IIT flyover,","city":"Delhi","state":"Delhi","lat":28.635308,"lng":77.22496,"distance":0},"categories":[],"verified":false,"stats":{"checkinsCount":1,"usersCount":1},"hereNow":{"count":0}},{"id":"4d9bdc2b913a236a4930a908","name":"Mocha @ Wills India Fashion Week","contact":{},"location":{"city":"New Delhi","state":"Delhi","lat":28.623567,"lng":77.247569,"distance":2566},"categories":[{"id":"4bf58dd8d48988d16d941735","name":"Café","pluralName":"Cafés","icon":"https://foursquare.com/img/categories/food/cafe.png","parents":["Food"],"primary":true}],"verified":false,"stats":{"checkinsCount":7,"usersCount":4},"hereNow":{"count":0}},{"id":"4b272979f964a520048524e3","name":"Sagar Ratna South Indian Restaurant","contact":{},"location":{"address":"Ashoka Hotel","city":"New Delhi","state":"Delhi","lat":28.646297,"lng":77.213513,"distance":1657},"categories":[],"verified":false,"stats":{"checkinsCount":7,"usersCount":7},"hereNow":{"count":0}},{"id":"4dce153a52b1f8915b8e7697","name":"H.I.S. India","contact":{"phone":"01145161111"},"location":{"lat":28.633064031600952,"lng":77.22141444683075,"distance":427},"categories":[{"id":"4bf58dd8d48988d1f6931735","name":"General Travel","pluralName":"General Travel","icon":"https://foursquare.com/img/categories/travel/default.png","parents":["Travel Spots"],"primary":true}],"verified":false,"stats":{"checkinsCount":1,"usersCount":1},"hereNow":{"count":0}}]},{"type":"tags","name":"Matching Tags","items":[{"id":"4b78c1faf964a52016e12ee3","name":"ITC Maurya, New Delhi","contact":{"phone":"911126112233"},"location":{"address":"Diplomatic Enclave, Sardar Patel Marg","city":"New Delhi","state":"Delhi","postalCode":"110 021","lat":28.597201785564085,"lng":77.1738052368164,"distance":6556},"categories":[{"id":"4bf58dd8d48988d1fa931735","name":"Hotel","pluralName":"Hotels","icon":"https://foursquare.com/img/categories/travel/hotel.png","parents":["Travel Spots"],"primary":true}],"verified":true,"stats":{"checkinsCount":346,"usersCount":198},"specials":[{"id":"4deda815814d56e30832ca8f","type":"frequency","message":"Earn 250 bonus Starpoints® when you check in with a confirmed reservation after you've linked your SPG and foursquare accounts at spg.com/socialcheckins.","finePrint":"SPG and foursquare accounts must be linked at time of check in. See spg.com/foursquare for additional terms and conditions.","icon":"check-in","title":"Check-in Special","provider":"foursquare","redemption":"standard"}],"hereNow":{"count":0}}]}]}}

我想在数据库中存储一个大的json响应,然后想要解析那个json。但问题是如何在不丢失实际格式的情况下将json存储在db中。当我将它作为文本存储在轨道上的ruby中时,在获取时我收到错误即无效的Json。

venue_list = VenuesList.create(:response=>@response)  /@response is the json above.
call_rake :parse_venue, :venue_list_id => venue_list.id

佣金任务: -

task :parse_venue => :environment do
venue_list = VenuesList.find(ENV["VENUE_LIST_ID"])
p response = venue_list.response // here this return invalid JSON.I tested the response on http://jsonlint.com/

response['response']['groups'][0]['items'].each do |venue| // here this return invalid JSON.

   p venue['name']
   p venue['location']['address']
   //........
 end
end

请查看内联评论

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

也许你可以使用Marshal.dump()和Marshal.load()。