从哈希中提取值 [英] Extracting values from a hash

查看:139
本文介绍了从哈希中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Im使用HTTParty来调用一个远程json文件,我需要提取这个URL以用于我的一个测试。
json格式如下所示:

 manifest:{
header:{
generated:xxxxxxxxxxxxxxx,
name: xxxxxxxxxxx,
version:1.0.0
},
files:[{
file:blimp.zip,
url:http://www.xxx.xx/restaurants_blimp.zip,
校验和:ee98c9455b8d7ba6556f53256f95
},{
file:yard.zip ,
url:www.xxx.xx / yard.zip,
校验和:e66aa3d123f804f34afc622b5
}

在irb上我可以得到所有的子哈希示例:['manifest'] ['files'],我只能得到url,如果我预先指定哪个一个..比如放文件['manifest'] ['files'] ['1'] ['url']< - 这对irb有用,但是因为我需要获取所有网址,这就是为什么我se .each,但它给了我一个无法转换为字符串错误或类似的

 #!/ usr / bin / env ruby​​ 

需要'httparty'

HOST = ARGV [0]
ID = ARGV [1]
VERSION = ARGV [2]


class MyApi
include HTTParty
end

file = MyApi.get(http://#{HOST} / v1 / dc / manifest /# {ID} /#{VERSION})


file.each do | item |
puts item ['manifest'] ['files'] ['url']
end

无法正常工作,但我可以在IRB上做一个:

放置item ['manifest'] ['files'] [2] ['url'] < - 这会给我的网址,但与.each将只是抱怨不能转换为字符串或类似的

解决方案

请尝试以下操作:

 #!/ usr / bin / env ruby​​ 

需要'httparty'

(HOST,ID,VERSION)= ARGV

class MyApi
包含HTTParty
格式:json
结束

response = MyApi.get(http://#{HOST} / v1 / dc / manifest /#{ID} /#{VERSION})

把response.inspect

添加格式:json 告诉HTTParty将响应解析为JSON。然后你会得到一个散列,你可以正确地迭代。


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:

  "manifest" : {
    "header" : {
      "generated" : "xxxxxxxxxxxxxxx",
      "name" : "xxxxxxxxxxx",
      "version" : "1.0.0"
    },
    "files" : [ {
      "file" : "blimp.zip",
      "url" : "http://www.xxx.xx/restaurants_blimp.zip",
      "checksum" : "ee98c9455b8d7ba6556f53256f95"
    }, {
      "file" : "yard.zip",
      "url" : "www.xxx.xx/yard.zip",
      "checksum" : "e66aa3d123f804f34afc622b5"
    }

on irb I can get all the sub hashes inside example: ['manifest']['files'] and I can only get the url if I expecify which one.. like for example puts file['manifest']['files']['1']['url'] <-- this does work on irb but since I need to get ALL url's this is why I use .each but it gives me a cant convert to string error or similar

#!/usr/bin/env ruby

require 'httparty'

HOST=ARGV[0]
ID=ARGV[1]
VERSION=ARGV[2]


class MyApi
  include HTTParty
end

file = MyApi.get("http://#{HOST}/v1/dc/manifest/#{ID}/#{VERSION}")


file.each do |item|
 puts item['manifest']['files']['url']
end

not working but I can on IRB do a:

puts item['manifest']['files'][2]['url'] <-- and this will give me the url but with the .each will just complaint about cant convert to string or similar

解决方案

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.

这篇关于从哈希中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆