1

Using Httparty, I get the following response

@parsed_reponse = > {"items" => {"@no" => "1", "@type" => "book", "@category" => "GENERAL" }}

when i use the following code to access @no, I am getting the following error: "Can't convert String into Integer"

r = @response.parsed_response["items"]["@no"]

Can anyone help on this? I do get "Can't convert String into Integer" or "Undefined method `[]' for nil:NilClass (NoMethodError)"

Can anyone explain this with an example?

4

1 回答 1

0

我不是 HTTParty 专家,但这里有一个我在响应中遍历所有数据的示例:

class Items
  include HTTParty
  default_params :output => 'xml'
  format :xml
  base_uri '<base_uri>'
end

xml =  Items.get('<xmlFile>')

xml["items"].each do |item|
  puts Integer(item['@no'])
end
于 2012-01-28T02:44:22.877 回答