且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将XML转换为JSON?

更新时间:2023-02-14 10:47:35

尽管可以使用PITA,但以下方法可以工作.代表每个标签s如下:

The following will work, though is a PITA to work with. Represent each tag s follows:

{
  attr => {...},
  tag => "...",
  content => [...]
}

content作为文本数组(在标签外部)或其他标签.

And content as an array of text (outside of tags) or else tags.

忽略空白和缩进,您的代码片段将变成:

Ignoring whitespace and indentation your snippet would become something like:

{
  tag => "p",
  content => [
    "We beg to send us immediately [...]",
    {
      tag => "note",
      content => [
        {
          tag => "p",
          content => [ "In the original, [...]" ]
        }
      ]
    },
    { tag => "lb" },
    {
      tag => "add",
      content => [ "by post" ],
    },
    " one copy of ",
    {
      tag=> "title",
      content => [ "A Book" ],
    },
    " by ",
    {
      tag => "persName",
      content => [
        {
          tag => "choice",
          content => [ ... ]
        }
      ],
    },
    ...
  ]
}

(抱歉,我很无聊.)

请注意,数据结构非常重复且冗长.但是您将以编程方式处理JSON,因此对于数据结构而言,可预测性和规则性非常好.

Note that the data structure is very repetitive and verbose. But you'll be processing the JSON programmatically, and for that it is very useful that the data structure is perfectly predictable and regular.