How can I convert JSON to XML in Ruby?

前端 未结 4 1549
有刺的猬
有刺的猬 2021-02-20 18:28

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

4条回答
  •  面向向阳花
    2021-02-20 19:04

    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)
    

    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.

提交回复
热议问题