2011-06-05 48 views
0

我使用batchbook ruby​​ gem来构建一个简单的列表页面,从我的batchbook crm中拉出各个字段。如果我拉一个给定公司的所有属性,我结束了这样的事情:如何从Batchbook CRM中的SuperTags中的自定义字段获取信息

{"id"=>13, "name"=>"John Deer", "notes"=>nil, "small_image"=>nil, "large_image"=>nil, "tags"=>[#<BatchBook::Tag:0x00000001df6438 @attributes={"id"=>"1002", "name"=>"portfolio", "supertag"=>"true", "fields"=>#<BatchBook::Tag::Fields:0x00000001dee5a8 @attributes={"logo"=>"img/fr-logo-button-sm.png", "description"=>"We make tractors."}, @prefix_options={}>}, @prefix_options={}>], "locations"=>[#<BatchBook::Location:0x00000001dea048 @attributes={"id"=>14, "label"=>"main", "primary"=>true, "email"=>"***@johndeer.com", "website"=>"http://johndeer.com", "phone"=>nil, "cell"=>nil, "fax"=>nil, "street_1"=>nil, "street_2"=>nil, "city"=>nil, "state"=>nil, "postal_code"=>nil, "country"=>nil}, @prefix_options={}>], "mega_comments"=>[], "created_at"=>"Thu Jun 02 22:32:16 UTC 2011", "updated_at"=>"Thu Jun 02 22:40:03 UTC 2011"} 

如何可以解析这个从我的“投资组合”中拉只是“标志”或只是“说明” SuperTag的?

也许是简单的使用只是@company.supertag对象,这给了我这样的:

[{"id"=>15, "name"=>"portfolio", "fields"=>{"logo"=>"img/fr-logo-button-sm.png", "description"=>"We make tractors."}}] 

但同样,我怎么能拉出各个字段“标志”或“说明”?

我觉得这应该是一个简单的过程,也许我要么在语法上挣扎,要么使得它比需要的更加复杂,但是您能否帮我解决问题?

+0

第一个字符串从batchboo调用属性方法对我公司来了对象k宝石。看起来我很困惑,试图按照正确的顺序混合散列和数组。谢谢您的帮助! – Fla2122 2011-06-06 20:33:32

+0

不是我所知道的。我所做的只是将公司对象存储为@company,然后将其称为@ company.attributes,试图查看所有可用的东西。 (看起来像我必须做出2个独立的http请求来获取我的超级标签信息,以及如何设置batchbook的API) – Fla2122 2011-06-08 19:34:04

回答

0

我不知道你的第一哈希像字符串是怎么一回事,所以我们将看看@company.supertag产值:

[ 
    { 
     "id"  => 15, 
     "name" => "portfolio", 
     "fields" => { 
      "logo"  => "img/fr-logo-button-sm.png", 
      "description" => "We make tractors." 
     } 
    } 
] 

我把它重新格式化为可读的自由。外部方括号表示它是Array,大括号进入下一级,因此Array包含Hash。无论Array和Hash使用[]来访问他们的元素,因此,如果整个数据结构是在a,我们记住一个Array在0索引开始,我们开始与此:

a[0]['fields'] 

这给了我们这个内哈希:

"fields" => { 
    "logo"  => "img/fr-logo-button-sm.png", 
    "description" => "We make tractors." 
} 

而且把它一步:

logo = a[0]['fields']['logo'] 
desc = a[0]['fields']['description'] 
相关问题