代码之家  ›  专栏  ›  技术社区  ›  Mark Szymanski

如何在Ruby中将JSON转换为XML?

  •  10
  • Mark Szymanski  · 技术社区  · 15 年前

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

    4 回复  |  直到 15 年前
        1
  •  11
  •   rwilliams    15 年前
    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”一词作为根,这不太好看。

        2
  •  7
  •   the Tin Man    15 年前

    关于@rwilliams aka r-dub答案:

    ActiveSupport moved its components 分成不同的模块进行粒度分析。与其一次加载所有内容,我们可以告诉它只加载某些子集,或者,如果我们仍然选择,我们可以一次加载所有内容。不管怎样,我们不能用 require 'activesupport' 就像我们以前一样,我们必须用 require 'activesupport/all' 或其中一个子集。

    >> 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"
    

    此外,ActiveSupport包含JSON支持,因此您可以使用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"
    

    第一行加载XML和JSON转换。第二行设置了一个用于测试的JSON示例。第三行接受假装的JSON,对其进行解码,然后将其转换为XML。

        3
  •  2
  •   Community Mohan Dere    9 年前

    其他答案不允许简单的递归转换。如中所述 this answer on Code Review ,您将需要一个自定义助手来创建您要查找的简单格式。

    它会把这个。。。

    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' } ]
          } ]
      },
    ]
    

    ……变成这样:

    <?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>
    
        4
  •  -1
  •   VP.    15 年前

    我不知道有什么神奇的宝石可以做到这一点,但您可以轻松地做的是xml到hash和hash到json。

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

    然后

    require 'json'
    my_json = my_hash.to_json
    
    推荐文章