ruby解析json、xml

来源:互联网 发布:中国历代位面之子知乎 编辑:程序博客网 时间:2024/05/16 14:53

ruby解析json、xml

解析xml
[ruby] view plain copy
  1. def get_xml_element(url,xml_element)  
  2.   require 'net/http'   
  3.   response=Net::HTTP.get_response(URI('http://api.platform.com/cinema/halls/format/xml/cid/1/pid/10000'))    
  4.   #~ puts response.code  
  5.   s = response.body  
  6.   #~ puts s  
  7.   s.scan(/\<#{xml_element}\>(.*)\<\/#{xml_element}\>/)  
  8. end  
  9. url='http://api.platform.com/cinema/halls/format/xml/cid/1/pid/10000'  
  10. puts get_xml_element(url,'errorMessage')  




解析json

[ruby] view plain copy
  1. require 'net\http'  
  2. require 'json'  
  3. response=Net::HTTP.get_response(URI('http://api.platform.com/cinema/halls/format/json/cid/1/pid/10000'))  
  4. puts response.body  
  5. data=response.body  
  6. result=JSON.parse(data)  
  7. p result  
  8. puts result['res']['errorMessage']  
0 0
原创粉丝点击