一个简单的把戏:
首先,您需要
gem install json,然后在使用Rails时可以执行以下操作:
require 'json'require 'active_support/core_ext'Hash.from_xml('<variable type="product_pre">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/')).bodyputs Hash.from_xml(s).to_json


