如何使用activeresource解析嵌套的json属性

时间:2012-10-22 22:35:33

标签: ruby json rest activeresource padrino

我试图通过来自rest-api的padrino框架中的activeresource解析以下jsons响应:

{"result":"Ok","resource":"ProjectRazor::Slice::Model","errcode":0,"command":"models_query_all","response":[{"@uri":"http://192.168.0.50:8026/razor/api/model/5nBqgTngqvxPIxVJJpdeQm","@callback":{},"@version":2,"@uuid":"5nBqgTngqvxPIxVJJpdeQm","@description":"Ubuntu Precise Model","@domainname":"andreasheumaier.de","@counter":0,"@broker_plugin":"agent","@root_password":"/7bbNwa6","@log":[],"@policy_bound":null,"@hostname_prefix":"firebird","@classname":"ProjectRazor::ModelTemplate::UbuntuPrecise","@osversion":"precise","@is_template":false,"@final_state":"os_complete","@node":null,"@req_metadata_hash":{"@domainname":{"validation":"^[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9](\\.[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])*$","example":"example.com","required":true,"description":"local domain name (will be used in /etc/hosts file)","default":"localdomain"},"@root_password":{"validation":"^[\\S]{8,}","example":"P@ssword!","required":true,"description":"root password (> 8 characters)","default":"test1234"},"@hostname_prefix":{"validation":"^[a-zA-Z0-9][a-zA-Z0-9\\-]*$","example":"node","required":true,"description":"node hostname prefix (will append node number)","default":"node"}},"@hidden":false,"@name":"ubuntu_precise","@label":"install_precise","@current_state":"init","@result":null,"@template":"linux_deploy","@noun":"model","@image_prefix":"os","@image_uuid":"4VuDj4mJDHP6bvWeztfXOO"}],"http_err_code":200}

我定义了这个模型

class RazorJsonFormatter
  include ActiveResource::Formats::JsonFormat

  def decode(json)
    ActiveResource::Formats::JsonFormat.decode(json)['response']
 end  
end

class Razor < ActiveResource::Base
  include ActiveResource::Formats::JsonFormat
  include ActiveResource::Extend::WithoutExtension

  self.format = RazorJsonFormatter.new
  self.site = "http://yellowbird:8026/razor/api/"
end
class RazorModel < Razor
  set_collection_name 'model'    
end

这会出现以下错误:

RazorModel.all
NameError: wrong constant name @callback
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1428:in `const_defined?'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1428:in `find_or_create_resource_for'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1278:in `block in load'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1264:in `each'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1264:in `load'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1006:in `initialize'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:933:in `new'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:933:in `instantiate_record'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `block in instantiate_collection'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `collect!'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `instantiate_collection'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:901:in `find_every'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:814:in `find'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:826:in `first'
    from (irb):27
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/lib/padrino-core/cli/base.rb:68:in `console'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/task.rb:27:in `run'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:120:in `invoke_task'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor.rb:275:in `dispatch'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:109:in `invoke'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/lib/padrino-core/cli/base.rb:73:in `c'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/task.rb:27:in `run'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:120:in `invoke_task'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor.rb:275:in `dispatch'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/base.rb:425:in `start'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/bin/padrino:9:in `<top (required)>'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/bin/padrino:19:in `load'
    from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/bin/padrino:19:in `<main>'1.9.3p194
(...)

这适用于预先设定的ActiveResource架构。我目前的解决方法是使用rest-client

从头开始编写所有类

如何使用activeresource解析-or igore-嵌套的json属性?

0 个答案:

没有答案