How can I convert JSON to XML in Ruby?

喜欢而已 提交于 2019-12-04 02:17:45
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.

Regarding @rwilliams aka r-dub answer:

ActiveSupport moved its components into separate modules for granularity. Rather than load everything all at once, we can tell it to load only certain subsets, or, if we still choose, we can load everything at once. No matter what, we can not use require 'activesupport' like we used to, instead we have to use require 'activesupport/all' or one of the subsets.

>> require 'active_support/core_ext/array/conversions' #=> true
>> [{:a => 1, :b => 2}, {:c => 3}].to_xml
=> "<?xml version="1.0" encoding="UTF-8"?>\n<objects type="array">\n  <objects a="1" b="2" type="hash"/>\n  <objects c="3" type="hash"/>\n</objects>\n"

In addition, ActiveSupport contains JSON support, so you can do the entire conversion with AR:

>> require 'active_support/all' #=> true
>> json = {'foo'=>'bar'}.to_json #=> "{"foo":"bar"}"
>> ActiveSupport::JSON.decode(json).to_xml #=> "<?xml version="1.0" encoding="UTF-8"?>\n<hash>\n  <foo>bar</foo>\n</hash>\n"

The first line loads in the XML and JSON conversions. The second line sets up a JSON sample to use for testing. The third line takes the pretend JSON, decodes it, then converts it to XML.

Noel Baron

The other answers do not allow for simple recursive conversions. As explained in this answer on Code Review, you'll need a custom helper to create the simple format you're looking for.

It will turn this...

data = [
  { 'name' => 'category1',
    'subCategory' => [
      { 'name' => 'subCategory1',
        'product' => [
          { 'name' => 'productName1',
            'desc' => 'desc1' },
          { 'name' => 'productName2',
            'desc' => 'desc2' } ]
      } ]
  },
  { 'name' => 'category2',
    'subCategory' => [
      { 'name' => 'subCategory2.1',
        'product' => [
          { 'name' => 'productName2.1.1',
            'desc' => 'desc1' },
          { 'name' => 'productName2.1.2',
            'desc' => 'desc2' } ]
      } ]
  },
]

...into this:

<?xml version="1.0"?>
<root>
  <category>
    <name>category1</name>
    <subCategory>
      <name>subCategory1</name>
      <product>
        <name>productName1</name>
        <desc>desc1</desc>
      </product>
      <product>
        <name>productName2</name>
        <desc>desc2</desc>
      </product>
    </subCategory>
  </category>
  <category>
    <name>category2</name>
    <subCategory>
      <name>subCategory2.1</name>
      <product>
        <name>productName2.1.1</name>
        <desc>desc1</desc>
      </product>
      <product>
        <name>productName2.1.2</name>
        <desc>desc2</desc>
      </product>
    </subCategory>
  </category>
</root>

i don't know a magic gem to do it, but what you can do easily is xml to hash and hash to json.

require 'active_support'
my_hash = Hash.from_xml(my_xml)

then

require 'json'
my_json = my_hash.to_json
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!