2011-01-05 5 views
5

매우 간단한 질문 (초보자입니다.) 나는 FB 포함 된 이름과 ID에서 JSON 응답이 있습니다레일 : JSON에서 값을 추출하는 방법

[{"name"=>"John Kline", "id"=>"10276192"}, {"name"=>"Quinn Kumbers", 
"id"=>"18093781"}, {"name"=>"Dan Jacobs", "id"=>"100000918716828"}] ... 

을 어떻게 추출하고 그 구조를 유지하면서 내 레일 응용 프로그램이 데이터에 액세스 할 수 있습니까? 레일즈에 "제 2 입국의 이드를주세요"또는 "제 275 번째 입국을주세요"라고 말하고 싶습니다. 이런 종류의 것들.

응답 할 때 아무 지식이 없으십시오. 고마워! 다른 보석없이

답변

8

:

ActiveSupport::JSON.decode(your_json)

6
# HT Omar Qureshi 
data = ActiveSupport::JSON.decode(your_json) 

# with the id of the 2nd entry 
do_something_with(data[1]['id']) 

# with the 275th entry 
do_something_else_with(data[274]) 

# loop over all the results 
data.each do |datum| 
    puts "#{datum['id']}: #{datum['name']}" 
end 
관련 문제