E
E
eldar_web2016-05-07 14:27:02
Ruby on Rails
eldar_web, 2016-05-07 14:27:02

How to parse xml or json page in Ruby?

I tried different options, I just can’t calculate the value of at least one element.
Here at this link https://geocode-maps.yandex.ru/1.x/?geocode=Tverskaya+6 - returns the answer in the form of xml,
and at https://geocode-maps.yandex.ru/1.x /?format=json&ge... - as json.
There is no difference, which option you choose, and now you could display the value of the element (tag) text - where is the address written? How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2016-05-07
@eldar_web

require 'json'
require 'net/http'
require 'uri'

def open(url)
  Net::HTTP.get(URI.parse(url))
end

json = open('https://geocode-maps.yandex.ru/1.x/?format=json&geocode=%D0%A2%D0%B2%D0%B5%D1%80%D1%81')
data = JSON.parse(json)
puts data['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['metaDataProperty']['GeocoderMetaData']['text']
ideone.com/2aE2iL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question