Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby中复杂哈希值的提取_Ruby_Hash_Zillow - Fatal编程技术网

Ruby中复杂哈希值的提取

Ruby中复杂哈希值的提取,ruby,hash,zillow,Ruby,Hash,Zillow,我使用的API(zillow)返回一个复杂的散列。示例结果如下所示 {"xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation"=>"http://www.zillow.com/static/xsd/SearchResults.xsd http://www.zillowstatic.com/vstatic/5985ee4/static/xsd/SearchResults.xsd",

我使用的API(zillow)返回一个复杂的散列。示例结果如下所示

{"xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", 
 "xsi:schemaLocation"=>"http://www.zillow.com/static/xsd/SearchResults.xsd http://www.zillowstatic.com/vstatic/5985ee4/static/xsd/SearchResults.xsd", 
 "xmlns:SearchResults"=>"http://www.zillow.com/static/xsd/SearchResults.xsd", "request"=>[{"address"=>["305 Vinton St"], "citystatezip"=>["Melrose, MA 02176"]}],
 "message"=>[{"text"=>["Request successfully processed"], "code"=>["0"]}],
 "response"=>[{"results"=>[{"result"=>[{"zpid"=>["56291382"], "links"=>[{"homedetails"=>["http://www.zillow.com/homedetails/305-Vinton-St-Melrose-MA-02176/56291382_zpid/"], 
 "graphsanddata"=>["http://www.zillow.com/homedetails/305-Vinton-St-Melrose-MA-02176/56291382_zpid/#charts-and-data"], "mapthishome"=>["http://www.zillow.com/homes/56291382_zpid/"], 
 "comparables"=>["http://www.zillow.com/homes/comps/56291382_zpid/"]}], "address"=>[{"street"=>["305 Vinton St"], "zipcode"=>["02176"], "city"=>["Melrose"], "state"=>["MA"], "latitude"=>["42.466805"], 
 "longitude"=>["-71.072515"]}], "zestimate"=>[{"amount"=>[{"currency"=>"USD", "content"=>"562170"}], "last-updated"=>["06/01/2014"], "oneWeekChange"=>[{"deprecated"=>"true"}], "valueChange"=>[{"duration"=>"30", "currency"=>"USD", "content"=>"42749"}], "valuationRange"=>[{"low"=>[{"currency"=>"USD", 
 "content"=>"534062"}], "high"=>[{"currency"=>"USD", "content"=>"590278"}]}], "percentile"=>["0"]}], "localRealEstate"=>[{"region"=>[{"id"=>"23017", "type"=>"city", 
 "name"=>"Melrose", "links"=>[{"overview"=>["http://www.zillow.com/local-info/MA-Melrose/r_23017/"], "forSaleByOwner"=>["http://www.zillow.com/melrose-ma/fsbo/"],
 "forSale"=>["http://www.zillow.com/melrose-ma/"]}]}]}]}]}]}]}
我可以使用以下方法提取特定值:

result = result.to_hash

p result["response"][0]["results"][0]["result"][0]["zestimate"][0]["amount"][0]["content"]

以这种方式指定每个元素的索引似乎很奇怪。有没有一种更简单的方法来获取命名值?

因为它确实是XML转储到JSON中,所以您可以使用它来查询JSON,看起来应该将其解析为XML。根据,它默认返回XML。显然,“to_hash”能够将其转化为一个hash(尽管这是一个非常难看的hash),但通过这种方式,您确实在试图逆流而上。我建议在开始时按预期使用它(xml),然后可能在以后将其解析为更易于使用的格式(如JSON/哈希结构)

非常擅长解析XML!可以使用xpath语法从dom甚至css选择器中获取元素

例如,要在每个结果中获取“内容”数组:

response = #get xml response from zillow
results = Nokogiri::XML(response).remove_namespaces!
#using css
content_array = results.css("result content")
#same thing using xpath:
content_array = results.xpath("//result//content")
如果只需要第一个结果中的内容,可以通过快捷方式执行此操作:

content = results.at_css("result content").content

我相信API返回的是XML,而不是哈希。。。您最好在xml上使用XPath,而不是将其解析为hashPerfect!这就是我最终得到的结果:results=Nokogiri::XML(response)!p zestimate=results.css(“result zestimate amount”).children().to_a[0].content.to_I基于我在回答中链接到的zillow文档页面。。。你能做到:results.at_css(“result zestimate amount”).content.to_i吗?