【问题标题】:How to convert Linkedin client in rails to Hash如何将 Rails 中的 Linkedin 客户端转换为 Hash
【发布时间】:2012-04-24 14:36:02
【问题描述】:

我这样创建linkedin客户端。

 client = LinkedIn::Client.new("3333", "rrrrrrr")
  client.authorize_from_access(session[:atoken], session[:asecret])

并像这样获取个人资料信息

 @profile = client.profile

当我打印像“puts client.profile”这样的配置文件时,我得到以下输出

 #<LinkedIn::Profile:0x4a77770 @doc=#<Nokogiri::XML::Document:0x253bb64 name="document" children=[#<Nokogiri::XML::Element:0x253b9fc name="pers
on" children=[#<Nokogiri::XML::Text:0x253b87c "\n  ">, #<Nokogiri::XML::Element:0x253b84c name="first-name" children=[#<Nokogiri::XML::Text:0x
253b684 "Anthony">]>, #<Nokogiri::XML::Text:0x253b5ac "\n  ">, #<Nokogiri::XML::Element:0x253b57c name="last-name" children=[#<Nokogiri::XML::
Text:0x253b3b4 "Raj S">]>, #<Nokogiri::XML::Text:0x253b2dc "\n  ">, #<Nokogiri::XML::Element:0x253b2ac name="headline" children=[#<Nokogiri::X
ML::Text:0x253b0e4 "Senior Software Engineer at AB Innovative Software Pvt. Ltd">]>, #<Nokogiri::XML::Text:0x253b00c "\n  ">, #<Nokogiri::XML:
:Element:0x253afdc name="site-standard-profile-request" children=[#<Nokogiri::XML::Text:0x253ae14 "\n    ">, #<Nokogiri::XML::Element:0x253ade
4 name="url" children=[#<Nokogiri::XML::Text:0x253ac1c "http://www.linkedin.com/profile?viewProfile=&key=78718913&authToken=XE_V&authType=name
&trk=api*a178337*s186521*">]>, #<Nokogiri::XML::Text:0x253ab44 "\n  ">]>, #<Nokogiri::XML::Text:0x253aa6c "\n">]>]>>

如何将其解析为 hasmap 或字符串?

请帮助我,因为我是 Rails 新手。

【问题讨论】:

标签: ruby-on-rails nokogiri linkedin


【解决方案1】:
  1. 获取 Nokogiri XML 文档

    xml = @profile.instance_variable_get :@doc
    
  2. 将 Nokogiri XML 文档转换为哈希:
    https://stackoverflow.com/a/10144623/405017

如果你想要一个不同的哈希形式,那么你应该在问题中指定你想要的输出。 :p 有很多方法可以将 XML 文档表示为哈希。

【讨论】:

    【解决方案2】:
    1. 将配置文件转换为 xml 2.用 nokogiri 操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多