Ruby 中有一个库可以将 XML 转换为 JSON 吗?
一个简单的技巧:
首先你需要gem install json
,那么当使用 Rails 时你可以这样做:
require 'json'
require 'active_support/core_ext'
Hash.from_xml('<variable type="product_code">5</variable>').to_json #=> "{\"variable\":\"5\"}"
如果你没有使用 Rails,那么你可以gem install activesupport
,需要它,事情应该顺利进行。
Example:
require 'json'
require 'net/http'
require 'active_support/core_ext/hash'
s = Net::HTTP.get_response(URI.parse('https://stackoverflow.com/feeds/tag/ruby/')).body
puts Hash.from_xml(s).to_json
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)