Ruby XML到JSON转换器? [英] Ruby XML to JSON Converter?

查看:215
本文介绍了Ruby XML到JSON转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在一个库,可以将Ruby中的XML转换为JSON?

Is there a library to convert XML to JSON in Ruby?

推荐答案

一个简单的技巧:

首先您需要gem install json,然后在使用Rails时可以执行以下操作:

First you need to gem install json, then when using Rails you can do:

require 'json'
require 'active_support/core_ext'
Hash.from_xml('<variable type="product_code">5</variable>').to_json #=> "{\"variable\":\"5\"}"

如果您不使用Rails,则可以按gem install activesupport要求进行操作,事情应该会顺利进行.

If you are not using Rails, then you can gem install activesupport, require it and things should work smoothly.

示例:

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

这篇关于Ruby XML到JSON转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆