2012-03-02 3 views
1

토큰을 얻은 후 페이스 북에서 사용자 세부 사항을 요청하고 있습니다. 나는 다음과 같은 반응을 얻었다. 응답문자열의 백 슬래시를 제거하십시오.

"{\"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

아니 걱정을 통보하지 않았다. 감사합니다. 감사합니다. –

관련 문제