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

发布于 2024-10-04 11:16:46 字数 35 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不爱素颜 2024-10-11 11:16:47

其他答案不允许简单的递归转换。正如代码审查的这个答案中所解释的,您需要一个自定义帮助器来创建您想要的简单格式寻找。

它将把这个......变成

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>

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>
唔猫 2024-10-11 11:16:47

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

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

然后

require 'json'
my_json = my_hash.to_json

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
风铃鹿 2024-10-11 11:16:46
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 的根参数。如果您不指定根,它将使用“哈希”一词作为根,这看起来不太好。

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.

迷路的信 2024-10-11 11:16:46

关于 @rwilliams aka r-dub 答案:

ActiveSupport 将其组件移动到单独的模块中以实现粒度化。我们可以告诉它只加载某些子集,而不是一次加载所有内容,或者,如果我们仍然选择,我们可以一次加载所有内容。无论如何,我们不能像以前那样使用 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。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文