提取嵌套在Ruby中的哈希数组的值

时间:2021-09-09 00:54:54

I have an XML response from a URL that is being converted into an array of hashes which looks like:

我有一个来自URL的XML响应,该URL被转换为哈希数组,如下所示:

{
  "EmployeeList"=>{
    "EmployeeProfile"=>{
      "BuildLoc"=>{"$"=>"1 Happy Place"},
      "Status"=>{"$"=>"A"},
      "SecrTitle"=>[{}, {}],
      "ID"=>{},
      "bct"=>{},
      "NUM"=>{"$"=>"1234567"},
      "BuildCity"=>{"$"=>"Dayton"},
      "BuildFloor"=>{"$"=>"6"},
      "Expense"=>{"$"=>"1345"},
      "LastName"=>{"$"=>"Smith"},
      "Middle"=>{},
      "SecrName"=>[{}, {}],
      "InternalSMTPAddress"=>{"$"=>"Joe.Smith@happy.com"},
      "IAddress"=>{"$"=>"Joe.Smith@happy.com"},
      "PreferredLastName"=>{},
      "DisplayName"=>{"$"=>"Joe Smith"},
      "CellPhoneNo"=>{},
      "Title"=>{"$"=>"Dr."},
      "BuildStreetAddress"=>{"$"=>"123 Happy town"},
      "BuildState"=>{"$"=>"IL"},
      "FirstName"=>{"$"=>"Joe"},
      "AltContactTitle1"=>{},
      "Dept-CostCtrNo"=>{"$"=>"129923"},
      "PreferredFirstName"=>{"$"=>"Joe"},
      "AltContactName2"=>{},
      "AltContactPhone2"=>{},
      "GDP"=>{},
      "BuildZip"=>{"$"=>"112345"},
      "RegionID"=>{"$"=>"NAMR"},
      "EmploymentType"=>{"$"=>"E"},
      "TempPhone"=>{},
      "BuildID"=>{"$"=>"01114"},
      "CountryAbbr"=>{"$"=>"USA"},
      "FaxDisp1"=>{},
      "BuildCountry"=>{"$"=>"United States"}
    }
  },
  nil=>nil
}

What's the easiest way to extract the value of "DisplayName" and "InternalSMTPAddress"?

提取“DisplayName”和“InternalSMTPAddress”值的最简单方法是什么?

2 个解决方案

#1


1  

If you assign the returned hash to a variable named "hash" you can access the two desired values for those keys like:

如果将返回的哈希值分配给名为“hash”的变量,则可以访问这些键的两个所需值,例如:

hash['EmployeeList']['EmployeeProfile']['DisplayName']
=> {"$"=>"Joe Smith"}

and

hash['EmployeeList']['EmployeeProfile']['InternalSMTPAddress']
=> {"$"=>"Joe.Smith@happy.com"}

If you want the actual data in them add a trailing ['$']:

如果你想要它们中的实际数据,请添加一个尾随['$']:

hash['EmployeeList']['EmployeeProfile']['DisplayName']['$']
=> "Joe Smith"

hash['EmployeeList']['EmployeeProfile']['InternalSMTPAddress']['$']
=> "Joe.Smith@happy.com"

#2


0  

If you need to find some key in nested hashes use this method:

如果需要在嵌套哈希中找到一些键,请使用以下方法:

def find_key(hash,key)
  hash.each {|k, v|
    return v if k==key
    tmp=find_key(v,key) if v.is_a?(Hash)
    return tmp unless tmp.nil?
  }
  return nil
end

Usage:

用法:

hash = Hash.new
hash["key1"] = "value1"
hash["key2"] = "value2"
hash["key3"] = Hash.new
hash["key3"]["key4"] = "value4"
hash["key3"]["key5"] = "value5"
hash["key6"] = Hash.new
hash["key6"]["key7"] = "value7"
hash["key6"]["key8"] = Hash.new
hash["key6"]["key8"]["key9"] = "value9"

find_key(hash,"key9") => "value9"
find_key(hash,"key8") => {"key9"=>"value9"}
find_key(hash,"dsfsdfsd") => nil

#1


1  

If you assign the returned hash to a variable named "hash" you can access the two desired values for those keys like:

如果将返回的哈希值分配给名为“hash”的变量,则可以访问这些键的两个所需值,例如:

hash['EmployeeList']['EmployeeProfile']['DisplayName']
=> {"$"=>"Joe Smith"}

and

hash['EmployeeList']['EmployeeProfile']['InternalSMTPAddress']
=> {"$"=>"Joe.Smith@happy.com"}

If you want the actual data in them add a trailing ['$']:

如果你想要它们中的实际数据,请添加一个尾随['$']:

hash['EmployeeList']['EmployeeProfile']['DisplayName']['$']
=> "Joe Smith"

hash['EmployeeList']['EmployeeProfile']['InternalSMTPAddress']['$']
=> "Joe.Smith@happy.com"

#2


0  

If you need to find some key in nested hashes use this method:

如果需要在嵌套哈希中找到一些键,请使用以下方法:

def find_key(hash,key)
  hash.each {|k, v|
    return v if k==key
    tmp=find_key(v,key) if v.is_a?(Hash)
    return tmp unless tmp.nil?
  }
  return nil
end

Usage:

用法:

hash = Hash.new
hash["key1"] = "value1"
hash["key2"] = "value2"
hash["key3"] = Hash.new
hash["key3"]["key4"] = "value4"
hash["key3"]["key5"] = "value5"
hash["key6"] = Hash.new
hash["key6"]["key7"] = "value7"
hash["key6"]["key8"] = Hash.new
hash["key6"]["key8"]["key9"] = "value9"

find_key(hash,"key9") => "value9"
find_key(hash,"key8") => {"key9"=>"value9"}
find_key(hash,"dsfsdfsd") => nil