如何将单个子XML元素转换为Json Array [英] How to convert single child xml element to Json Array
问题描述
我正在使用WSO2 ESB,并尝试将XML有效负载转换为Json.
I am using WSO2 ESB and trying to convert my XML payload to Json.
<property name="messageType" value="application/json" scope="axis2"/>
上述属性介体将我的xml转换为json,并且一切正常.
The above property mediator convert my xml to json and it all works fine.
问题出在我的XML有效负载中的子节点上.
The issue is with the child nodes in my XML payload.
当xml是
<users>
<user>user1</user>
<user>user2</user>
</users>
它转换为
"users": {
"user": [
"user1", "user2"
]
}
所以我剩下的接收JSON有效负载的完整端点(期望列表用户"工作正常).
so my rest full endpoint recieving the json payload which is expecting a list 'user' works fine.
但是当xml是
<users>
<user>user1</user>
</users>
转换后的json看起来像这样,
the converted json looks like this,
"users": {
"user": "user1"
}
因此,期望用户"列表的restfull端点没有得到列表,而是发送了一个字符串,并且数据类型不匹配导致找不到端点.
So the restfull endpoint which is expecting a list of 'user' is not getting a list rather a string is sent and the datatype mismatch causes endpoint not found.
如果进一步尝试,
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://james.newtonking.com/projects/json">
<users>
<user json:Array="true">user1</user>
</users>
</Data>
此转换为json,
{
"Data": {
"users": {
"user": {
"@Array": "true",
"$": "user1"
}
}
}
}
我需要的是
{
"Data": {
"users": {
"user": {
[
"user1"
]
}
}
}
}
在杰伊的建议之后,
感谢杰伊,
在您输入之后,我尝试了一些操作,但是在某些时候遇到了困难.这就是我正在尝试的,
After your inputs, I tried something but I am stuck at some point. This is what I am trying,
mc.setPayloadJSON(
{
"someIds" : {
"someIdList" : (vUIdLen > 1 ? mc.getProperty("someIdList") : "["+someIdList+"]")
}
});</script>
我正在检查子节点的长度,如果大于1,则使用该节点的较早捕获的值["abc","pqr"],并且小于或等于1比我正在使用单个json值并将其构造在"[" + someIdList +]"中,但它们中的任何一个都没有被追加.它给出的错误为脚本引擎返回了执行内联js脚本函数中介的错误".
I am checking the lenth of the child nodes and if it is greater than 1 than I am using the earlier captured value for that node which is ["abc","pqr"] and if it is less than or = 1 than i am using the single json value and constructing it within "["+someIdList+"]" but either of them are not getting appended. it is giving error as "The script engine returned an error executing the inlined js script function mediate".
如何正确附加此内容.
(vUIdLen > 1 ? mc.getProperty("someIdList") : "["+someIdList+"]")
上面的mc.getProperty("someIdList")的值为["abc","pqr"],"[" + someIdList +]"中的someIdList的值为abc.
The value of mc.getProperty("someIdList") above is ["abc","pqr"] and value of someIdList in "["+someIdList+"]" comes as abc.
请提出建议.
推荐答案
还有另一种无需使用脚本介体的解决方案,您可以添加
There is another solution for this without using script mediator, you can add
<?xml-multiple?>
对您的有效载荷的处理指令.如下;
processing instruction to your payload. as follows;
<users>
<?xml-multiple?>
<user>user1</user>
</users>
这将为用户创建json数组.
this will create json array for users.
{"users": {"user": ["user1"]}}
希望这会有所帮助.
这篇关于如何将单个子XML元素转换为Json Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!