【问题标题】:ruby - how to parse this LinkedIn's hash?ruby - 如何解析这个LinkedIn的哈希?
【发布时间】:2012-04-07 11:16:58
【问题描述】:

从 LinkedIn API 我得到了这个哈希:

> #<LinkedIn::Mash all=[#<LinkedIn::Mash api_standard_profile_request=#<LinkedIn::Mash headers=#<LinkedIn::Mash
> all=[#<LinkedIn::Mash name="x-li-auth-token" value="name:_nNI">]
> total=1> url="http://api.linkedin.com/v1/people/asdasfga">
> first_name="first name" headline="job position"
> id="id of user" industry="industry"
> last_name="last name" location=#<LinkedIn::Mash
> country=#<LinkedIn::Mash code="en"> name="country">
> site_standard_profile_request=#<LinkedIn::Mash
> url="http://www.linkedin.com/profile?viewProfile=&key=key_number&authToken=_nNI&authType=name&trk=api*a_12346*s_12346*">>]
> total=1>

我尝试获取first_nameurlcountry 已经很长时间了,但我仍然无法解析它。 谁能帮帮我?

提前致谢

编辑次尝试:

linkedin.connections.each do |item|
   puts item
end

给予

total
1
all
#<LinkedIn::Mash api_standard_profile_request=#<LinkedIn::Mash headers=#<LinkedIn::Mash all=[#<LinkedIn::Mash name=...

当我尝试时

linkedin.connections.api_standard_profile_request.each do |item|
   puts item
end

然后我得到了

undefined method `each' for nil:NilClass

【问题讨论】:

  • 请查看更新后的帖子。我不知道如何进入api_standard_profile_request 哈希。
  • linkedin.connections 是什么类型的对象?那是您在问题开头打印的对象吗?
  • 您可以调用.to_hash 将 Mash 转换为 Hash(如 params)。 linkedin.to_hash

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


【解决方案1】:

试试这个:

linkedin.all.each do |profile|
  puts profile.first_name
end

【讨论】:

    【解决方案2】:

    我首先建议您使用p 而不是puts 来调试对象。它在您正在打印的对象上调用 inspect 而不是 to_s,这会为您提供有关该对象的更多信息。

    接下来,查看Hashie::Mash 上的文档。你的 LinkedIn::Mash 对象继承自这个类,但 LinkedIn 没有提供太多文档,所以你必须沿着继承链走。

    由于Hashie::Mash 是一个类似哈希的对象,您应该在迭代它时使用each_pair 来获取哈希中的每个键值对,而不是一次获取一个键或值:

    linkedin.connections.each_pair do |key, value|
      p key
      p value
    end
    

    这些都不能真正回答您的问题,但希望您可以从调试工作中获得更多信息。我猜你需要做这样的事情:

    linkedin.connections[0].url
    

    但我并不积极。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-10
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多