如何在Ruby中将JSON转换为XML? [英] How can I convert JSON to XML in Ruby?

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

问题描述

有没有办法在Ruby中将JSON转换为XML?

Is there any way to convert JSON to XML in Ruby?

推荐答案

require 'active_support' #for to_xml() 'gem install activesupport' use the 2.3 branch
require 'json' #part of ruby 1.9 but otherwise 'gem install json'

my_json = "{\"test\":\"b\"}"
my_xml = JSON.parse(my_json).to_xml(:root => :my_root)

还要注意to_xml的根参数.如果您不指定根,它将使用单词"hash"作为根,看起来不太好.

Also note the root argument of to_xml. If you don't specify a root it'll use the word 'hash' as the root which isn't very nice to look at.

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

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