私は PHP プログラマーで、最近 Rails に切り替えました。
JSON 出力を配列に変換する際に問題が発生しています。
PHPでは、json_decode
関数をよく使用しました。JSON 文字列を PHP 配列に簡単に変換できますが、Rails ではそのようなタイプの関数を見つけることができませんでした。
これは私のJSONです:
{"posts":[{"post":{"id":"1","createdOn":"2012-11-07 07:09:03","updatedOn":"2013-01-14 02:39:55","restaurantname":"Sapori","contractno":"307","listingtype":"Full","address":"Valley Road,","location":"Birkirkara","phone":"21494679","website":"N\/A","cuisine":",32,33,","onetimeofferexlunch":",50:monday,50:tuesday,50:wednesday,50:thursday,50:friday,50:saturday,50:sunday,","onetimeofferexdinner":",50:monday,50:tuesday,50:wednesday,50:thursday,50:friday,50:saturday,50:sunday,","ongoingdiscountlunch":"","ongoingdiscountdinner":"","unlimitedofferinfo":"Unlimited Discount 15% on the food bill for the whole table.","reservationrequired":"No","desertincluded":"Yes","membershipspertable":"Unlimited","openingclosing":"Open 09:00-23:00 everyday.","amenities":",,","description":"","fbcode":"","mark_new":"No","ownerfname":"David Testing for check tv webservice","ownermname":"","ownerlname":"Xuereb","designation":"Owner","ownerphone":"79428796","owneremail":"saporicafe@melita.com","ownerpassword":"sSj1eQnngA","uploadmenu":"","serialno":"0","status":"1","latitude":"35.896368","longitude":"14.463627","rate_review_code":"1RtKdrqFHe","imgurl_0":"http:\/\/v3.gusto.com.mt\/upload\/image\/0\/image73_1322041350.jpg","Rating Message":"Rated 8.2 by 78 Members","Average":"8.2","cuisine_1":"Pasta","cuisine_2":"Pizzeria"}}]}
私はこのコードを使用しています:
@result= open("http://v3.gusto.com.mt/webservice/getrestaurnatdetails.php?id=1&format=json").read
@result = ActiveSupport::JSON.decode(@result)
@result["posts"].each{|p| @test=puts p["restaurantname"] }
puts"@@@@@@@@@@@@@@@@@@@@@@@@@@ RESTAURANT #{@test.inspect}"
しかし、それは機能していません。どうすれば入ることができrestaurantname
ます@test
か?