2012-03-02 11 views
1

トークンを取得した後で、ユーザーの詳細をFacebookにリクエストしています。私は以下の反応を得た。 応答文字列のバックスラッシュを削除します。

"{\"id\":\"xxxxx\",\"name\":\"abcd\",\"first_name\":\"ab\",\"last_name\":\"cd\", 
\"link\":\"http:\\/\\/www.facebook.com\\/profile.php?id=xxxxx\",\"quotes\":\"Life is a difficult game. You can win it only by retaining your birthright to be a person.\",\"gender\":\"female\",\"timezone\":5.5,\"locale\":\"en_US\",\"verified\":true,\"updated_time\":\"2012-02-22T12:59:39+0000\"}" 

を次のように私は"String"として応答示すのクラスをプリント。私はこれをハッシュに変更したい。 (上記の\を削除します)

私は試みましたが、正しいフォーマットを取得しませんでした。

答えて

8

これはJSONです。あなたはそれを解析する必要があります。

require 'json' 

JSON.parse("{\"id\":\"xxxxx\",\"name\":\"abcd\",\"first_name\":\"ab\",\"last_name\":\"cd\", 
\"link\":\"http:\\/\\/www.facebook.com\\/profile.php?id=xxxxx\",\"quotes\":\"Life is a difficult game. You can win it only by retaining your birthright to be a person.\",\"gender\":\"female\",\"timezone\":5.5,\"locale\":\"en_US\",\"verified\":true,\"updated_time\":\"2012-02-22T12:59:39+0000\"}") 

#=> {"id"=>"xxxxx", "name"=>"abcd", "first_name"=>"ab", "last_name"=>"cd", "link"=>"http://www.facebook.com/profile.php?id=xxxxx", "quotes"=>"Life is a difficult game. You can win it only by retaining your birthright to be a person.", "gender"=>"female", "timezone"=>5.5, "locale"=>"en_US", "verified"=>true, "updated_time"=>"2012-02-22T12:59:39+0000"} 
+0

でそれを書いていた – visnu

4

回答はJSONオブジェクトです。これをハッシュに解析する最も簡単な方法は、JSON gemを使用する方法です。文字列の最初の数個のエンティティを使った例を示します。ご覧のとおり、ハッシュを返します。

ruby-1.9.3-rc1 :001 > require 'json' 
=> true 
ruby-1.9.3-rc1 :002 > JSON.parse("{\"id\":\"xxxxx\",\"name\":\"abcd\",\"first_name\":\"ab\"}") 
=> {"id"=>"xxxxx", "name"=>"abcd", "first_name"=>"ab"} 
+0

申し訳ありませんが、私はあなたの応答:)私たちは、おそらく私はこれを試し同時に:) – p0deje

+0

ありません心配に気付きませんでした。感謝します。 –

関連する問題