我有一个包含以下内容的 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" }
]
}
}