mule - 映射数组或对象JSON响应

时间:2018-02-27 17:56:30

标签: java json mapping mule

我调用一个Web服务,在成功的情况下返回一个json数组:

[
  {"name":"a"},
  {"name":"b"}
]

如果失败,它会返回一个对象:

{
 "status":"Failed",
 "describtion":"Error occured"
}

如何映射两个响应以便处理它们?

3 个答案:

答案 0 :(得分:1)

您可以根据Web服务响应代码使用不同的转换。

这样的事情:

<choice> 
  <when expression="#[message.inboundProperties['http.status'] == 200]">
    <!-- transform success response -->
  </when>
  <otherwise>
    <!-- transform failure response -->
  </otherwise>
</choice>

答案 1 :(得分:0)

您可以使用

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

答案 2 :(得分:0)

在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
}