将具有可变属性的 json 对象转换为 xml 数组
Api响应是{“Content”:{“634331”:[“无法找到产品”],“634332”:[“无法找到产品”],“等...
我无法捕获这些值将内容转换为 xml 后:
<Content>
<__634331>Product could not be found</__634331>
<__634332>Product could not be found</__634332>
<__123104398>Product could not be found</__123104398>
值被解释为字段名称。
有没有办法将 json 对象转换为 xml 数组,如下所示:
<Content>
<res>
<key>634331</key>
<value>Product could not be found</value>
</res>
<res>
<key>634332</key>
<value>Product could not be found</value>
</res>
The Api response is {"Content":{"634331":["Product could not be found"],"634332":["Product could not be found"],"etc…
and I am having trouble to catch the values after content into xml :
<Content>
<__634331>Product could not be found</__634331>
<__634332>Product could not be found</__634332>
<__123104398>Product could not be found</__123104398>
The values are being interpreted as field names.
Is there a way to convert json object into xml array looking like:
<Content>
<res>
<key>634331</key>
<value>Product could not be found</value>
</res>
<res>
<key>634332</key>
<value>Product could not be found</value>
</res>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 JSON 中,映射(或“对象”)中的键表示数据值而不是属性名称是很常见的。不幸的是,这根本不能很好地映射到 XML,其中等效的结构通常是这样的结构:
没有自动转换器能够识别这种转换是合适的。
我的建议是使用 XSLT 3.0 模板规则进行自定义转换。我需要查看您的 JSON 的更多详细信息以及所需的 XML,以便提供更详细的建议。
It's quite common in JSON for keys in a map (or "object") to represent data values rather than property names. Unfortunately this doesn't map at all well to XML, where the equivalent would usually be a structure like
No automatic converter is going to be able to recognise that this kind of conversion is appropriate.
My recommendation would be to do a custom conversion using XSLT 3.0 template rules. I would need to see more detail of your JSON and required XML to advise in more detail.