How can I convert JSON to XML in Ruby?

前端 未结 4 1535
有刺的猬
有刺的猬 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.

    0 讨论(0)
  • 2021-02-20 19:11

    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
    
    0 讨论(0)
  • 2021-02-20 19:13

    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>
    
    0 讨论(0)
  • 2021-02-20 19:22

    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.

    0 讨论(0)
提交回复
热议问题