2014-01-22 5 views
0

해시로 변환 한 API의 JSON 응답이 있습니다. 키 제목, 디렉터, 개요 및 배열의 ​​값을 추출하고 싶습니다. 가용성 및 트레일러. 나는 추출물을 시도했다! 슬라이스 모두 키 값 조합을 찾는 데 어려움이있는 것 같습니다.Rails/Ruby에서 복잡한 해시에서 단일 값 쌍을 추출합니다.

{"availability"=>{}, "created"=>"2013-01-08T08:02:04.349441+00:00", "data"=>{"adult"=>false, "alternative_titles"=>[], "backdrop_path"=>"https://image.tmdb.org/t/p/w1280/eN5XFV9fyLfoTsBDKssrS4fu515.jpg", "belongs_to_collection"=>{"backdrop_path"=>"/zvZBNNDWd5LcsIBpDhJyCB2MDT7.jpg", "id"=>8650, "name"=>"Transformers Collection", "poster_path"=>"/giURBCO3r4LVbhywEjIuYz92fEv.jpg"}, "budget"=>0, "cast"=>[{"character"=>"Trypticon (voice)", "id"=>15831, "name"=>"Frank Welker", "profile_path"=>"https://image.tmdb.org/t/p/w185/a3QPvpgqKMGViS2M9mGcRy7xDZ.jpg"}, {"character"=>"Scourge (voice)", "id"=>15860, "name"=>"Miguel Ferrer", "profile_path"=>"https://image.tmdb.org/t/p/w185/fvRncqShcaV8R45IJGYZ7IAvrsO.jpg"}, {"character"=>"Powerglide (voice)", "id"=>27993, "name"=>"Jeffrey Combs", "profile_path"=>"https://image.tmdb.org/t/p/w185/eAwMDGN99JAjbkDY2KkH0e00Hct.jpg"}, {"character"=>"Thrust (voice)", "id"=>6576, "name"=>"Mark Rolston", "profile_path"=>"https://image.tmdb.org/t/p/w185/bsh3cqDNwVvux4NdaY1Bj4S7mNS.jpg"}, {"character"=>nil, "id"=>13240, "name"=>"Mark Wahlberg", "profile_path"=>"https://image.tmdb.org/t/p/w185/eGHgl0sRg0ecKuAiHKehPUnY1lL.jpg"}, {"character"=>"", "id"=>61186, "name"=>"Nicola Peltz", "profile_path"=>"https://image.tmdb.org/t/p/w185/ywS7lEeN6kbZCOUH3U2Rxiw1DFc.jpg"}, {"character"=>"", "id"=>2283, "name"=>"Stanley Tucci", "profile_path"=>"https://image.tmdb.org/t/p/w185/omGlTJF2IW5r3L3c5y0qkCt3hFr.jpg"}], "crew"=>[{"department"=>"Directing", "id"=>865, "job"=>"Director", "name"=>"Michael Bay", "profile_path"=>"https://image.tmdb.org/t/p/w185/cJNLMOqIkGZDdwkwSq1YpgiMp9P.jpg"}], "directors"=>[{"department"=>"Directing", "id"=>865, "job"=>"Director", "name"=>"Michael Bay", "profile_path"=>"https://image.tmdb.org/t/p/w185/cJNLMOqIkGZDdwkwSq1YpgiMp9P.jpg"}], "genres"=>[{"id"=>878, "name"=>"Science Fiction"}], "homepage"=>"", "id"=>91314, "imdb_id"=>"tt2109248", "original_title"=>"Transformers: Age of Extinction", "overview"=>"Fourth part of the Transformers series, which starts a new main characters changing history.", "popularity"=>4.98715854566441, "poster_path"=>"https://image.tmdb.org/t/p/w342/k8utnXyWgMXKz6QVTOfwMsNrAJk.jpg", "production_companies"=>[], "production_countries"=>[{"iso_3166_1"=>"US", "name"=>"United States of America"}], "release_date"=>"2014-06-27", "releases"=>{"AR"=>{"rating"=>"", "release_date"=>"2014-07-10"}, "AU"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "BE"=>{"rating"=>"", "release_date"=>"2014-06-25"}, "BR"=>{"rating"=>"", "release_date"=>"2014-07-19"}, "CH"=>{"rating"=>"", "release_date"=>"2014-07-17"}, "CL"=>{"rating"=>"", "release_date"=>"2014-07-10"}, "DE"=>{"rating"=>"", "release_date"=>"2014-07-17"}, "DK"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "ES"=>{"rating"=>"", "release_date"=>"2014-08-08"}, "FR"=>{"rating"=>"", "release_date"=>"2014-07-02"}, "GB"=>{"rating"=>"", "release_date"=>"2014-07-10"}, "HK"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "HU"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "IT"=>{"rating"=>"", "release_date"=>"2014-07-16"}, "LT"=>{"rating"=>"", "release_date"=>"2014-06-27"}, "MX"=>{"rating"=>"", "release_date"=>"2014-07-11"}, "NL"=>{"rating"=>"", "release_date"=>"2014-07-09"}, "NO"=>{"rating"=>"", "release_date"=>"2014-06-27"}, "PE"=>{"rating"=>"", "release_date"=>"2014-07-10"}, "PT"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "RU"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "SE"=>{"rating"=>"", "release_date"=>"2014-06-27"}, "SG"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "TR"=>{"rating"=>"", "release_date"=>"2014-06-27"}, "US"=>{"rating"=>"", "release_date"=>"2014-06-27"}}, "revenue"=>0, "runtime"=>0, "spoken_languages"=>[{"iso_639_1"=>"en", "name"=>"English"}], "status"=>"Planned", "tagline"=>"", "title"=>"Transformers: Age of Extinction", "trailers"=>{"id"=>91314, "quicktime"=>[], "youtube"=>[]}, "vote_average"=>8.0, "vote_count"=>1}, "key"=>"p30RBh.transformers-4", "last_mod"=>"2014-01-19T21:52:32.021978+00:00", "last_refresh"=>"2014-01-19T21:52:32.012421+00:00", "lookup_key"=>"p30RBh.transformers-age-of-extinction", "matches"=>{"netflix"=>"catalog/titles/movies/70299855"}, "path"=>"p30RBh/transformers-age-of-extinction", "release_timestamp"=>1403827200, "salt"=>"p30RBh", "sort_keys"=>{"as_is"=>"Transformers: Age of Extinction\u001Fp30RBh", "ignore_case"=>"transformers: age of extinction\u001Fp30RBh", "ignore_case_normalized"=>"transformers: age of extinction\u001Fp30RBh", "no_article"=>"transformers: age of extinction\u001Fp30RBh", "no_article_normalized"=>"transformers: age of extinction\u001Fp30RBh", "no_article_symbols"=>"transformers age of extinction\u001Fp30RBh", "no_symbols"=>"transformers age of extinction\u001Fp30RBh"}, "source"=>{"id"=>91314, "service"=>"themoviedb"}, "version"=>15} 

답변

2

당신은 extract 또는 slice 필요는 없지만, 그 JSON의 제목을 찾기 위해, 당신은 단순히 다음과 같이 Hash[] 기능을 사용 :

hash["data"]["title"] 

한편, hash["data"]["directors"] 당신 도착 객체 배열 :

[{"department"=>"Directing", "id"=>865, "job"=>"Director", "name"=>"Michael Bay", "profile_path"=>"https://image.tmdb.org/t/p/w185/cJNLMOqIkGZDdwkwSq1YpgiMp9P.jpg"}] 

Netflix API 설명서를보고 마우스 키를보고 디버그를 사용하십시오. 콘솔에서 hash["data"]부터 시작하여 모든 해시에 keys을 호출하여 원하는 데이터로 트리를 탐색하는 방법을 알아낼 수 있습니다.

관련 문제