删除字符串中的斜杠
我在获得令牌后在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"
的响应类。 我想把它改成哈希。 (我想删除上面的\)。
我试过但没有得到正确的格式。
那是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"}
您获得的响应是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"}
- 使用script / delay_job创建delayed_job worker时出现未定义的方法错误
- 如何在Ruby on Rails中获取ActiveRecord执行的最后一个SQL查询?
- Ruby:递归方法
- Ruby map方法语法问题
- 开始吧。 。 。 结束而表示’阻止’?
- ArgumentError:未知密钥::条件。 有效密钥为:: class_name,:class,:foreign_key
- 耙子流产了! 用于ActiveRecord :: Base的未定义方法`migration_error =’:Class
- 如何使用ActiveSupport :: Configurable with Rails Engine
- 从Ruby 1.8.7到系统调用iconv将文件从utf-16转换为utf-8