我有一个
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" } ] } }