【问题标题】:Validating JSON response using rest client使用 rest 客户端验证 JSON 响应
【发布时间】:2017-10-26 19:40:43
【问题描述】:

我正在尝试在 ruby​​ 中使用 rest 客户端来解析 json,但我不知道如何验证响应(从响应中提取值)。另外我正在尝试验证有效的响应代码(200) response.code 不起作用。 以下是 JSON 响应和使用 rest 客户端获取它的代码:

def self.call_legacy_transactions

  get_response=RestClient::Request.execute(method: :get, url: 'URL', timeout: 15)
  puts(get_response.body)
  json_response = JSON.parse(get_response)
//Dont know how to get the values from response hash. Please suggest

  end

JSON 响应:

    [
  {
    "value": "12345678912345",
    "events": [
      {
        "transaction_id": 205,
        "package_name": "",
        "event_codes": [
          465,
          469,
          471,
          474,
          410,
          490,
          1040
        ]
      },
      {
        "transaction_id": 204,
        "package_name": "",
        "event_codes": [
          465,
          469,
          474,
          490
        ]
      },
      {
        "transaction_id": 207,
        "package_name": "",
        "event_codes": [
          465,
          469,
          471,
          474,
          490
        ]
      }
    ]
  }
]

我想要每个值的每个事务的事件代码。

【问题讨论】:

  • 您能否让我们更好地了解您想要的输出?只是一个事件代码列表?

标签: json ruby automated-tests cucumber rest-client


【解决方案1】:

如果您只需要事件代码整数的平面列表,您可以使用:

json_response.flat_map do |value_data|
  value_data[:events].flat_map do |event_data|
    event_data[:event_codes]
  end
end

更新基于评论“我只想提取事务 id 为 205 的事件代码”:

如果只有一件物品具有该交易 ID:

json_response.flat_map do |value_data|
  value_data[:events].find do |event_data|
    event_data[:transaction_id] == 205
  end[:event_codes]
end

如果可能有许多具有该交易 ID 的项目:

json_response.flat_map do |value_data|
  value_data[:events].select do |event_data|
    event_data[:transaction_id] == 205
  end.flat_map do |event_data|
    event_data[:event_codes]
  end
end

【讨论】:

  • 我只想提取事务 id 为 205 的事件代码
  • @RV_Dev,我已根据您的评论更新了我的答案。
【解决方案2】:

您可以在响应上调用方法以查看正文、响应代码等。更多信息请参阅README

添加到您的代码中:

def self.call_legacy_transactions(tx_id = 205)
  get_response=RestClient::Request.execute(method: :get, url: 'URL', timeout: 15)
  puts(get_response.body)

  # check if the response was successful
  if get_response.code == 200
    # need to parse the body
    json_response = JSON.parse(get_response.body)

    # the json response is an array of json objects
    # we need to iterate over them and grab the value
    # in the `events` key and iterate over those and
    # select just the ones with the desired transaction id and
    # get the value in each of the `event_codes` keys and
    # then flatten all the sub arrays into one
    event_codes = json_response.flat_map do |data|
      data['events'].
        select { |event| event['transaction_id'] == tx_id }.
        flat_map { |event| event['event_codes'] }
    end

    event_codes # is now a list of just the event codes
  end
end

通过上述方法,您可以将交易 id 传递给方法以获取任何交易的事件代码,例如

call_legacy_transactions 205 

【讨论】:

  • @DiegoSalazaar:我只想提取事务 id 为 205 的事件代码
  • @RV_Dev 更新了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
相关资源
最近更新 更多