0

I have this problem, where I keep on getting

TypeError: can't convert String into Integer

This is where I'm going through this @data object.

here is the @data object

@data = HTTParty.get("")

{
"body"=>{
    "predictions"=>{
        "direction"=>{
            "prediction"=>[
                {
                    "epochTime"=>"1362931090892",
                },
                {
                    "epochTime"=>"1362931747892",

                },
                {
                    "epochTime"=>"1362932467892",

                },
                {
                    "epochTime"=>"1362933187892",

                },
                {
                    "epochTime"=>"1362933847892",

                }
            ],
            "title"=>"xxxx"
        },
        "a"=>"xx",
        "b"=>"x"
    },
    "name"=>"some"
}

}

and my code to go through above has been

  <%  if @data["body"]["predictions"].present? %>
  <% @data["body"]["predictions"].each do |p| %>
        <%p["direction"].each do |d|%>
            <% d["prediction"].each do |k|%>
              <h4><%= k["epchoTime.."] %> </h4>
            <%end%>
        <%end%>
      <%end%>
<%end%>

I have no idea how to go through this, I assume this is due to the fact I should access stuff like I do in C++ with name[integer] value, but I would like to use ["name"]. How can I make my code work ?

Thanks for your precious time and consideration.

4

4 回答 4

1

下面是一个使用 Nokogiri 解析原始 XML 的示例:

require 'nokogiri'

doc = Nokogiri::XML(<<EOT)
<body>
  <predictions>
    <direction>
      <prediction>
        <epochTime>1362931090892</epochTime>
        <epochTime>1362931747892</epochTime>
        <epochTime>1362932467892</epochTime>
        <epochTime>1362933187892</epochTime>
        <epochTime>1362933847892</epochTime>
      </prediction>
      <title">xxxx</title>
      <a>"xx"</a>
      <b>"x"</b>
      <name>"some"</name>
    </direction>
  </predictions>
</body>
EOT

epoch_times = doc.search('epochTime').map(&:text)

它返回一个epochTime值数组:

[
    [0] "1362931090892",
    [1] "1362931747892",
    [2] "1362932467892",
    [3] "1362933187892",
    [4] "1362933847892"
]

有时我们需要遍历prediction包含块的所有epochTime块。这将做到:

epoch_times = doc.search('prediction').map{ |predict|
  predict.search('epochTime').map(&:text)
}

[
    [0] [
        [0] "1362931090892",
        [1] "1362931747892",
        [2] "1362932467892",
        [3] "1362933187892",
        [4] "1362933847892"
    ]
]

有时您需要找到一个特定的节点并在其中获取某种类型的所有元素:

doc = Nokogiri::XML(<<EOT)
<body>
  <predictions>
    <direction>
      <prediction id="1">
        <epochTime>1362931090892</epochTime>
        <epochTime>1362931747892</epochTime>
        <epochTime>1362932467892</epochTime>
        <epochTime>1362933187892</epochTime>
        <epochTime>1362933847892</epochTime>
      </prediction>
      <title">xxxx</title>
      <a>"xx"</a>
      <b>"x"</b>
      <name>"some"</name>
    </direction>
  </predictions>
</body>
EOT

epoch_times = doc.search('prediction[id="1"]').map{ |predict| predict.search('epochTime').map(&:text) }

[
    [0] [
        [0] "1362931090892",
        [1] "1362931747892",
        [2] "1362932467892",
        [3] "1362933187892",
        [4] "1362933847892"
    ]
]
于 2013-03-10T21:52:52.707 回答
0

铁皮人有一个很好的观点,就是把这个逻辑放在视野之外,应该是一个模型的作品。不过,这是一个更清晰的视图。

<% @data['body']['predictions']['direction']['prediction'].each do |x| %>
  <% x.each do |k, v| %>
    <h4><%= v %></h4>
  <% end %>
<% end %>
于 2013-03-10T19:07:50.527 回答
0

整个散列的事情已经失控,因为我不断检测每个人的存在并循环通过它,所以正如 Tin man 建议的那样,我使用 XML 'nokogiri' 然后使用 .css 方法来检测它是否存在并循环通过它,因为原始响应是在 xml 中。

  @doc.css('predictions').each do |predictions_node| 
        predictions_node.css('direction').each do |direction_node| 
          direction_node.css('prediction').each do |prediction|
            // stuff here 

感谢您的时间和考虑。

于 2013-03-10T21:43:21.637 回答
0

看起来您忽略了没有嵌套值的键,例如titleand a, b

        "title"=>"xxxx"
    },
    "a"=>"xx",
    "b"=>"x"
},

您的最终代码应如下所示

if @data["body"]["predictions"].present? 
 @data["body"]["predictions"].each do |p|
  p.each do |d|
   if d.kind_of? Hash
    d.each do |k| 
     if k.kind_of? Array
      k.each do |x|
        if x.kind_of? Array
          x.each do |y|
            if y.kind_of? Hash
              puts y["epochTime"]
            end
          end
        end
      end 
    end
    end
   end
  end
 end
end

上面的代码很难看,但你是 C++ 程序员,你可能会喜欢它:P

于 2013-03-10T20:26:54.997 回答