Extracting values from a hash

前端 未结 2 1465
傲寒
傲寒 2021-01-22 17:46

Hello Im using HTTParty to call for a remote json file that I need to extract the URL\'s to use in one of my tests.. the json format goes something like:

  \"man         


        
相关标签:
2条回答
  • 2021-01-22 18:29

    Try the following:

    #!/usr/bin/env ruby
    
    require 'httparty'
    
    (HOST, ID, VERSION) = ARGV
    
    class MyApi
      include HTTParty
      format :json
    end
    
    response = MyApi.get("http://#{HOST}/v1/dc/manifest/#{ID}/#{VERSION}")
    
    puts response.inspect
    

    The addition of the format :json tells HTTParty to parse the response as JSON. Then you'll get a hash you can iterate over properly.

    0 讨论(0)
  • 2021-01-22 18:33

    Try:

    file['manifest']['files'].each do |item|
       puts item['url']
    end
    
    0 讨论(0)
提交回复
热议问题