Передать параметр из Json Httparty в ostruct - PullRequest
0 голосов
/ 09 мая 2018

Я бы хотел взять данные из ответа, помещенного в коллекцию

require 'httparty'
require 'ostruct'
require 'nokogiri'

response = HTTParty.get('http://localhost:3000/api/v2/teste')
   #example response   {x:[{"testey":1213,"Testex":"2018-03-07"}]}

collection = [
  OpenStruct.new(
    :testex => '657758',
    :testey => 'CTH6536'
   )
]

Ответы [ 3 ]

0 голосов
/ 09 мая 2018

Я нашел решение

response = HTTParty.get('http://localhost:3000/api/v2/teste')

response['x'].each do |aux|
  @msgx = aux['Testex']
  @msgy = aux['testey']
end

collection = [
  OpenStruct.new(
    :testex => @msgx,
    :testey => @msgy
   )
]
0 голосов
/ 10 мая 2018

Твой ответ хорош Андрей-Баран, у меня проблема с этим методом "[" ответ из моего ответа: {x: [{"testey": 1213, "Testex": "2018-03-07"}]}

response = HTTParty.get ('http://localhost:3000/api/v2/teste')

Если бы ответ был так {x: {"testey": 1213, "Testex": "2018-03-07"}} сделал бы это

response = HTTParty.get('http://localhost:3000/api/v2/teste')
xy = JSON.parse(response.body, object_class: OpenStruct)
puts xy.x.testey
0 голосов
/ 09 мая 2018

Согласно документации http://ruby -doc.org / stdlib-2.0.0 / libdoc / json / rdoc / JSON.html # method-i-parse

JSON.parse(response.body, object_class: OpenStruct)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...