【问题标题】:Parsing Hash - Rails解析哈希 - Rails
【发布时间】:2012-12-13 04:38:03
【问题描述】:

我正在尝试解析来自 LinkedIn 的哈希,并遇到错误

这是我尝试解析的哈希(从 Mash 转换而来):

#<LinkedIn::Mash first_name="John" headline="Recruiter" id="xxxxxxxx" industry="Telecommunications" last_name="Smith" location=#<LinkedIn::Mash country=#<LinkedIn::Mash code="us"> name="Greater Philadelphia Area"> positions=#<LinkedIn::Mash all=[#<LinkedIn::Mash company=#<LinkedIn::Mash id=2325416 industry="Staffing and Recruiting" name="Amazing Associates, Inc" size="1-10 employees" type="Privately Held"> id=9999999 is_current=true start_date=#<LinkedIn::Mash month=6 year=2010> summary="" title="Corporate Recruiter">] total=1> public_profile_url="http://www.linkedin.com/pub/greatguyjohn"> 

这是我的代码:

<% if !connection["positions"].nil? %>
    <% connection["positions"].each do |positions| %>
        <% if !positions["all"].nil? %>
            <% positions["all"].each do |company| %>
                <%= company %>
            <% end %>
        <% end %>
    <% end %>
<% end %>

这是错误:

can't convert String into Integer
Extracted source (around line #15):
12:         <% if !connection["positions"].nil? %>
13:             <% connection["positions"].each do |positions| %>
14:                 <% if !positions["all"].nil? %>
15:                     <% positions["all"].each do |company| %>
16:                         <%= company %>
17:                     <% end %>

问题似乎是从 ["positions"] 散列中解析 ["all"] 散列。即使其余的哈希解析正常,我也会不断收到此错误。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 parsing hash


    【解决方案1】:

    connection["positions"] 在您的示例中不会返回数组。它返回一个 LinkedIn::Mash 哈希对象。你错误地调用了.each

    【讨论】:

    • 谢谢,不过我应该用什么代替?
    • 试试connection["positions"]["all"]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 2010-12-26
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 2013-02-04
    相关资源
    最近更新 更多