在 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
,要求它,事情应该会顺利进行。
示例:
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
关于xml - Ruby XML 到 JSON 转换器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1530324/