【问题标题】:Parsing Data from httpparty response从 httpparty 响应中解析数据
【发布时间】:2014-04-07 20:43:31
【问题描述】:

我从一个 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、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。

【问题讨论】:

  • HTTParty 为你做这件事。不要重新解析原始响应。显示您的 HTTParty 代码。
  • @MarkThomas - 嗨,马克,我已经编辑了我的问题以包含我目前拥有的 HTTParty 代码
  • 你有完整的课程吗?初始化器是什么样的?

标签: ruby-on-rails ruby json httparty


【解决方案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] 替换为循环/映射)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2012-01-28
    • 2022-01-12
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多