xml - Ruby XML 到 JSON 转换器?

标签 xml ruby json converter

在 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/

相关文章:

Python - 使用正则表达式更新 2 个节点之间的 xml 值 - re.sub 不起作用

c# - '对象'不包含 'GetSqlXml' 的定义

json - 获取 package.json 文件中另一个字段的值(npm)

java - 使用 json_exists 查询 JSON 的 JDBC 准备语句

python - 使用 ElementTree 添加子元素

php - 从 Outlook 中删除格式

ruby-on-rails - Rails validates_format_of with Regex 在换行的情况下失败

ruby - 在二维数组中注入(inject)增量计数器

ruby - 破折号星破折号 (-*-) 在 Ruby 文件中的作用是什么?

json - 在 Go 中从 POST 请求解码 JSON 失败