简体   繁体   English

在Ruby中查询JSON嵌套哈希响应时出现问题

[英]Problems querying JSON nested hash response in Ruby

Cannot seem to solve this problem: 似乎无法解决此问题:

I'm getting JSON nested hash responses from Lastfm and everything works fine when the response is structures as such: 我从Lastfm获取JSON嵌套哈希响应,并且当响应具有以下结构时,一切工作正常:

{"topalbums" =>{"album" =>{"name =>"Friday Night in Dixie"}}}

However if the artist does not have a top album the response is structured this way and I get a NoMethodError undefined method '[]' for nil:NilClass. 但是,如果艺术家没有顶级专辑,则响应将以这种方式构造,并且我得到了nil:NilClass的NoMethodError未定义方法'[]'。

{"topalbums" =>{"#text"=>"\n   ", "artist"=>"Mark Chestnutt"}}

What I want to do is query the response so I do not keep getting this error. 我想做的就是查询响应,所以我不会一直收到此错误。

Here is my method: 这是我的方法:

 def get_albums
   @albums = Array.new
   @artistname.each do |name|
      s = LastFM::Artist.get_top_albums(:artist => name, :limit => 1)
      r = JSON.parse(s.to_json)['topalbums']['album']['name']
      @albums.push(r)
   end
 end

which gives me exactly what I want if the artist has a top album, what I need to do is somehow add a condition to query the keys in the nested hash. 如果艺术家拥有一张顶级专辑,这正是我想要的。我需要做的是以某种方式添加条件来查询嵌套哈希中的键。 However, I cannot seem to grasp how to do this as when I add this line of code to check key values: 但是,我似乎无法掌握如何执行此操作,就像我添加以下代码行以检查键值时一样:

s.each_key { |key, value| puts "#{key} is #{value}" } 

the output I get is this: 我得到的输出是这样的:

topalbums is topalbums是

so topalbums key does not have a value associated with it. 因此,topalbums键没有与之关联的值。

This is what I have tried so far: 到目前为止,这是我尝试过的:

  def get_albums
   @albums = Array.new
   @artistname.each do |name|
      s = LastFM::Artist.get_top_albums(:artist => name, :limit => 1)
      if s.has_key?('album') #I know this won't work but how can I query this?
         r = JSON.parse(s.to_json)['topalbums']['album']['name']
         @albums.push r 
      else
         @albums.push(name << "does not have a top album")
      end
   end
 end

How can I fix this so I get 'Mark Chestnut does not have a top album' instead of the NoMethodError ? 如何解决此问题,以便获得“ Mark Chestnut没有热门专辑”而不是NoMethodError Cheers 干杯

Use Hash#fetch default values, I would do as below: 使用Hash#fetch默认值,我将执行以下操作:

No "album" key present 没有"album"

hash = {"topalbums" =>{"#text"=>"\n   ", "artist"=>"Mark Chestnutt"}}
default_album = {"name" => "does not have a top album"}
hash["topalbums"].fetch("album", default_album)["name"]
#=> "does not have a top album"

"album" key present "album"键存在

hash = {"topalbums" =>{"#text"=>"\n   ", "artist"=>"Mark Chestnutt", "album" => {"name" => "Foo"}}}
hash["topalbums"].fetch("album", default_album)["name"]
#=> "Foo"

So if the hash does not have an "album" key fetch defaults to default_album else it uses the key it find as in the second case 因此,如果散列没有"album"键,则fetch默认为default_album否则它将使用找到的键,如第二种情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM