Javascript 节点中的jstoxml转换器模块未以正确的结构分析数据
我正在尝试使用jstoxml模块在我的节点js服务中将JSON对象转换为XML。我的输入结构是:Javascript 节点中的jstoxml转换器模块未以正确的结构分析数据,javascript,xml,json,node.js,npm,Javascript,Xml,Json,Node.js,Npm,我正在尝试使用jstoxml模块在我的节点js服务中将JSON对象转换为XML。我的输入结构是: { "user": "505723c5750c1fa2177682ed", "uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items", "items": [ { "uri": "http://localhost:3000/items/1" }, {
{
"user": "505723c5750c1fa2177682ed",
"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items",
"items": [
{
"uri": "http://localhost:3000/items/1"
},
{
"uri": "http://localhost:3000/items/2"
}
],
"info": "blah."
}
我的预期结果是:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
505723C5050C1FA2177682ED
http://localhost:3000/users/505723c5750c1fa2177682ed/items
http://localhost:3000/items/1
http://localhost:3000/items/2
废话。
我得到的结果是:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
505723C5050C1FA2177682ED
http://localhost:3000/users/505723c5750c1fa2177682ed/items
http://localhost:3000/items/1
http://localhost:3000/items/2
废话。
如果有人遇到类似的问题,请帮助我。是否有其他npm节点包可用于在预期结构中实现此功能
感谢免责声明:我是一个开源JavaScript库的作者,用于XMLJS转换。Jsonix可用于Node.js(请参阅) 可以基于映射在XML和JSON(双向)之间进行转换。映射为您提供了在JSON和XML之间转换的灵活性。这可能就是你在这里需要的
我会让密码说出来的。以下是JSON->XML转换的演示案例:
Data
)和另一种类型Item
。Data
类型用于根XML元素Data
好,现在:
下面是您得到的XML:
<data>
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
</data>
505723c5750c1fa2177682ed
http://localhost:3000/users/505723c5750c1fa2177682ed/items
http://localhost:3000/items/1
http://localhost:3000/items/2
废话。
链接:
如果您可以将XML与JSON匹配,请检查其他库,例如。我找到了问题的解决方案。你可以得到它。这是一个在线演示。通过导出他们提供的代码,我们可以实现它,感谢您使用我的库!我为耽搁道歉。您可以通过以下结构实现该输出:
[
{"user": "505723c5750c1fa2177682ed"},
{"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items"},
[
{"items": {"uri": "http://localhost:3000/items/1"}},
{"items": {"uri": "http://localhost:3000/items/1"}}
],
{"info": "blah."}
]
输出:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<info>blah.</info>
505723C5050C1FA2177682ED
http://localhost:3000/users/505723c5750c1fa2177682ed/items
http://localhost:3000/items/1
http://localhost:3000/items/1
废话。
这是一个精确的json到xml的转换,为什么你希望它与众不同..谢谢你的评论。我需要的结果的格式,我的预期结果。有什么办法吗?。或者我需要在json中做什么更改才能得到我期望的结果??Naeem。。在中尝试此在线工具。这给了我预期的结果。干得好。。但我运气不好(但我不能创建映射文件。我的json可以是动态的。有什么解决方案吗?@Sreejith With Jsonix目前还没有。我将实现无映射解决方案,但目前这对您没有帮助。但是,检查一下,它列出了许多方法,也许您会找到符合您需要的方法。然后尝试我推荐的xml2js
)Oned@Sreejith将其作为答案发布(这样做是可以的)。
[
{"user": "505723c5750c1fa2177682ed"},
{"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items"},
[
{"items": {"uri": "http://localhost:3000/items/1"}},
{"items": {"uri": "http://localhost:3000/items/1"}}
],
{"info": "blah."}
]
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<info>blah.</info>