您可以使用 Json 转换调解器[1] 来实现您的用例。这里我们需要定义 json 模式来匹配预期的数据结构。对于上述情况,您将能够使用类似于以下的 json 模式。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"items": {
"type": "object",
"properties": {
"item": {
"type": "array",
"items": [
{
"type": "object"
}
]
}
},
"required": [
"item"
]
}
},
"required": [
"items"
]
}
这指定项目是一个数组。然后可以将模式添加到注册表 [2] 并在 json 转换中介中引用。 Json 转换中介需要在 xml 到 json 转换之后添加。请参考以下示例代理服务。
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="transform"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<property name="messageType" scope="axis2" value="application/json"/>
<property name="ContentType" scope="axis2" value="application/json"/>
<jsontransform schema="conf:/schema.json"/>
<respond/>
</inSequence>
</target>
<description/>
</proxy>
输入 1
<items>
<item><val1></val1></item>
<item><val1></val1></item>
</items>
输出 1
{
"items": {
"item": [
{
"val1": null
},
{
"val1": null
}
]
}
}
输入 2
<items>
<item><val1></val1></item>
</items>
输出 2
{
"items": {
"item": [
{
"val1": null
}
]
}
}
[1]-https://docs.wso2.com/display/EI660/JSON+Transform+Mediator
[2]-https://docs.wso2.com/display/EI660/Managing+the+Registry+