当前位置: 首页 > 工具软件 > XML to JSON > 使用案例 >

ruby解析json、xml

滕璞瑜
2023-12-01
解析xml
def get_xml_element(url,xml_element)
  require 'net/http' 
  response=Net::HTTP.get_response(URI('http://api.platform.com/cinema/halls/format/xml/cid/1/pid/10000'))  
  #~ puts response.code
  s = response.body
  #~ puts s
  s.scan(/\<#{xml_element}\>(.*)\<\/#{xml_element}\>/)
end
url='http://api.platform.com/cinema/halls/format/xml/cid/1/pid/10000'
puts get_xml_element(url,'errorMessage')




解析json

require 'net\http'
require 'json'
response=Net::HTTP.get_response(URI('http://api.platform.com/cinema/halls/format/json/cid/1/pid/10000'))
puts response.body
data=response.body
result=JSON.parse(data)
p result
puts result['res']['errorMessage']


 类似资料: