【问题标题】:Convert XML to JavaScript Object将 XML 转换为 JavaScript 对象
【发布时间】:2012-11-28 07:46:30
【问题描述】:

我有一个包含以下内容的 XML 文件:

<directory>
  <app>
    <title>Carrot</title>
    <url>www.carrot.com</url>
  </app>
  <app>
    <title>Cucumber</title>
    <url>www.cucumber.com</url>
  </app>
</directory>

假设我已经能够阅读它并将内容存储为字符串:

s = '<directory><app><title>Carrot</title><url>www.google.com</url></app><app><title>Cucumber</title><url>www.cucumber.com</url></app></directory>';

如何将其转换为如下的 JavaScript 对象?

{
  "directory": {
    "app": [
      { "title": "Carrot", "url": "www.carrot.com" },
      { "title": "Cucumber", "url": "www.cucumber.com" }
    ]  
  }
}

【问题讨论】:

标签: javascript jquery xml


【解决方案1】:

我用这个插件...http://www.thomasfrank.se/xml_to_json.html

它一直对我很有魅力。

【讨论】:

    【解决方案2】:

    我认为您正在寻找以下问题的答案Convert XML to JSON (and back) using Javascript

    XML Javascript 中的 JSON 转换

    引用的答案

    我认为这是最好的:Converting between XML and JSON

    请务必阅读 Xml.com O'Reilly 网站上的随附文章(链接到 > 底部)。作者详细介绍了这些转换的问题,我认为 > 你会发现这些问题很有启发性。 O'Reilly 主持这篇文章的事实应该表明 >Stefan 的解决方案是有价值的。

    【讨论】:

    • +1 表示“请务必阅读 Xml.com O'Reilly 网站上的随附文章(链接到 >底部)。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多