2012-03-02 110 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 2012-03-02 11:56:54

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 2012-03-02 11:46:12

+0

不用担心,我们在同一时间:) – 2012-03-02 11:50:18