0

我正在获取 xml 输出,然后我将该 xml 转换为 json 对象。格式如下所示。

{
"SOAP-ENV:Envelope": {
    "@xmlns:SOAP-ENV": "http://schemas.xmlsoap.org/soap/envelope/",
    "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "@xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
    "SOAP-ENV:Body": {
        "rpc:TestExampleResponse": {
            "@xmlns:rpc": "http://Test.com/asi/",
            "TestMessage": {
                "listOfTESTS": {
                    "@xmlns:xmlns": "http://www.Test.com/xml/TEST",
                    "TESTS": [{
                            "id": "1",
                            "lastSyncDate": "12/16/2015 07:06:38",
                            "listOfTESTsyncrealtimeChild": null
                        }, {
                            "id": "2",
                            "lastSyncDate": "12/16/2015 07:06:38",
                            "listOfTESTsyncrealtimeChild": null
                        }

                    ]
                }
            }
        }
    }
 }
}

我想从 Mulesoft 中的 JSON 输出中提取测试数组。我不知道如何在 mulesoft 中提取该数组。提前致谢

4

2 回答 2

0

您可以使用Dataweave(Anypoint Studio 中的转换消息组件)

(骡子EE)

看看文档:

https://docs.mulesoft.com/mule-user-guide/v/3.7/using-dataweave-in-studio

您输入的示例脚本:

%dw 1.0
%input payload application/json
%output application/json
---
TESTS: payload."SOAP-ENV:Envelope"."SOAP-ENV:Body"."rpc:TestExampleResponse".TestMessage.listOfTESTS.TESTS map ((tEST , indexOfTEST) -> {
        id: tEST.id,
        lastSyncDate: tEST.lastSyncDate,
        listOfTESTsyncrealtimeChild: tEST.listOfTESTsyncrealtimeChild
})

使用时的输出%output application/json

{
  "TESTS": [
    {
      "id": "1",
      "lastSyncDate": "12/16/2015 07:06:38",
      "listOfTESTsyncrealtimeChild": null
    },
    {
      "id": "2",
      "lastSyncDate": "12/16/2015 07:06:38",
      "listOfTESTsyncrealtimeChild": null
    }
  ]
}

使用时的输出%output application/java

{TESTS=[{id=1, lastSyncDate=12/16/2015 07:06:38, listOfTESTsyncrealtimeChild=null}, {id=2, lastSyncDate=12/16/2015 07:06:38, listOfTESTsyncrealtimeChild=null}]}
于 2016-01-07T13:57:30.580 回答
-2

您可以编写如下所示的自定义转换器。这个转换器使用Jackson (com.fasterxml.jackson)依赖。

转换器返回一个字符串列表,其中每个字符串代表TESTS数组的一个元素。

public class JsonArrayExtractor extends AbstractTransformer {

    private final ObjectMapper mapper = new ObjectMapper();
    private final String testsNodeJsonPointer = "/SOAP-ENV:Envelope/SOAP-ENV:Body/rpc:TestExampleResponse/TestMessage/listOfTESTS/TESTS";

    public JsonArrayExtractor() {
        registerSourceType(DataTypeFactory.STRING);
    }

    @Override
    protected Object doTransform(Object src, String enc) throws TransformerException {
        String payload = Objects.toString(src);

        JsonNode root;
        try {
            root = mapper.readTree(payload);
        } catch (IOException e) {
            throw new TransformerException(this, e);
        }

        List<String> testsList = new ArrayList<>();

        JsonNode testsNode = root.at(JsonPointer.valueOf(testsNodeJsonPointer));
        if (testsNode instanceof ArrayNode) {
            ArrayNode testsArrayNode = (ArrayNode) testsNode;
            for (JsonNode test : testsArrayNode) {
                testsList.add(test.toString());
            }
        }
        return testsList;
    }
}

您可以在流程中使用上述转换器,如下所示。

<custom-transformer class="org.ram.JsonArrayExtractor" doc:name="extractTestsArray"/>
于 2016-01-07T07:31:01.603 回答