加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将XML转换为JavaScript对象

发布时间:2020-12-16 22:57:50 所属栏目:百科 来源:网络整理
导读:我有一个 XML文件,其中包含以下内容: directory app titleCarrot/title urlwww.carrot.com/url /app app titleCucumber/title urlwww.cucumber.com/url /app/directory 假设我能够读取它并将内容存储为字符串: s = 'directoryapptitleCarrot/titleurlwww.g
我有一个 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" }
    ]  
  }
}

解决方法

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

它总是为我带来魅力.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读