[英]mule - mapping array or object JSON response
我调用一个在成功的情况下返回json数组的Web服务:
[
{"name":"a"},
{"name":"b"}
]
并且在失败的情况下,它返回一个对象:
{
"status":"Failed",
"describtion":"Error occured"
}
如何映射两个响应以便处理它们?
您可以根据Web服务响应代码使用不同的转换。
像这样:
<choice>
<when expression="#[message.inboundProperties['http.status'] == 200]">
<!-- transform success response -->
</when>
<otherwise>
<!-- transform failure response -->
</otherwise>
</choice>
您可以使用
JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("status");
if(status == null){
// Show error message
}else{
String statistics = json.getString("name");
}
Try this. Let me know if it not work. I'll give another solution
在HTTP之后在“转换消息”下面使用,您将获得正确的输出。
%dw 1.0
%output application/json
---
{
Status:"Success" when payload.status != 'Failed' otherwise "Failure",
Describtion:payload.describtion when payload.status == 'Failed' otherwise null,
Data:payload when payload.status != 'Failed' otherwise null
}
之后,您的输出将是
1,成功案例
{
"Status": "Success",
"Describtion": null,
"Data": [
{
"name": "a"
},
{
"name": "b"
}
]
}
2.故障案例
{
"Status": "Failure",
"Describtion": "Error occured",
"Data": null
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.