从httparty响应中解析数据

时间:2014-04-07 20:43:31

标签: ruby-on-rails ruby json httparty

我从http派对请求中返回了很多JSON

在我的Rails控制器中,我得到了这些数据:

 @data = @request.parsed_response["InfoResponses"]

在我的html.erb中,我在屏幕上显示了@data,如下所示:

[{"AdditionalResponse"=>nil, "AddressResponse"=>nil, "HighResResponse"=>nil, "LowResResponse"=>nil, "OtherResponse"=>nil, "Error"=>nil, "CarResponse"=>{"Values"=>[{"FieldDescription"=>"Colour", "FieldName"=>"COLOUR", "Value"=>BLUE}, {"FieldDescription"=>"Engine Size", "FieldName"=>"ENGINESIZE", "Value"=>1400}, {"FieldDescription"=>"Number Doors", "FieldName"=>"NUMBERDOORS", "Value"=>4}, {"FieldDescription"=>"Fuel", "FieldName"=>"FUEL", "Value"=>GAS}, {"FieldDescription"=>"Wheel Size", "FieldName"=>"WHEELSIZE", "Value"=>17}, {"FieldDescription"=>"CD Player", "FieldName"=>"CDPLAYER", "Value"=>Y}], 
"Error"=>nil, "Id"=>"2"}, "Id"=>"2", "DensityResponse"=>nil, "RiskIndexResponse"=>nil, "FinanceResponse"=>{"Value"=>Available, "Error"=>nil, "Id"=>"2"}}, 

{"AdditionalResponse"=>nil, "AddressResponse"=>nil, "HighResResponse"=>nil, "LowResResponse"=>nil, "OtherResponse"=>nil, "Error"=>nil, "CarResponse"=>{"Values"=>[{"FieldDescription"=>"Colour", "FieldName"=>"COLOUR", "Value"=>RED}, {"FieldDescription"=>"Engine Size", "FieldName"=>"ENGINESIZE", "Value"=>1400}, {"FieldDescription"=>"Number Doors", "FieldName"=>"NUMBERDOORS", "Value"=>4}, {"FieldDescription"=>"Fuel", "FieldName"=>"FUEL", "Value"=>GAS}, {"FieldDescription"=>"Wheel Size", "FieldName"=>"WHEELSIZE", "Value"=>19}, {"FieldDescription"=>"CD Player", "FieldName"=>"CDPLAYER", "Value"=>Y}], 
"Error"=>nil, "Id"=>"1"}, "Id"=>"1", "DensityResponse"=>nil, "RiskIndexResponse"=>nil, "FinanceResponse"=>{"Value"=>Available, "Error"=>nil, "Id"=>"1"}}]

在屏幕上我只想显示CarResponse详细信息 - 即Colout,EngineSize等所有字段及其值以及我要提取的唯一其他详细信息是FinanceResponse the Value和Id。

提取这些细节的最佳方法是什么 - 我在这里展示了2,但我的JSON响应可能包含与上述相同的响应。

编辑 - 我的HttpParty代码如下

def self.GetInforFromASPWebAPI
 @request_body = [{:Id => '1',
                    :A => '1',
                    :B => '1',
                    :C=> '1'}]

    post('localhost:50544/api/MyController/GetInfo', :body => @request_body.to_json,
                                     :headers => {'Content-Type' => 'application/json'})
end

这是一个Helper类 - 然后在我的控制器中我做了类似下面的事情:

@response = ASPWebAPIClass.GetInforFromASPWebAPI

响应的内容甚至是更多的数据,所以我使用了之前所做的将其中一部分删除到parsed_response。

1 个答案:

答案 0 :(得分:1)

您应该有一个代表API的完整类。然后,您的方法将获得您感兴趣的内容,而不是将整个响应传递回Rails控制器,从而强制控制器知道如何解析API的响应。根据单一责任原则将所有API知识放入API类中。结果你的代码会更清晰。

示例:

class CarAPI
  include HTTParty
  base_uri 'localhost:50544/api/MyController'
  # any other initialization stuff

  def initialize
    @options = {
       :body => @request_body.to_json,
       :headers => {'Content-Type' => 'application/json'}
    }
  end

  def car_info
    response = self.class.post '/getinfo'

    # Create a simple Ruby hash with key/value pairs
    cars = {}
    response['InfoResponses'][0]['CarResponses']['Values'].each do |field|
      cars[field['FieldName']] = field['Value']
    end
    cars 
  end
end

这有点简化;你想要迭代所有的响应(用循环/地图替换[0])。