Dal*_*ale 2 dataweave mulesoft
我的 API 有一个字符串输入。它可以是 XML 或 JSON,我需要在其上使用 Read 函数将其解析为 JSON。MuleSoft 4 或 Dataweave 2 中确定字符串格式的最佳方法是什么?
也许我可以写一个函数 isXML(String inputString) 或 isJSON(String inputString)。
到目前为止,我有一些不雅的东西。我正在尝试将输入解析为 XML 并捕获任何解析异常。在捕获中,我将尝试将其解析为 JSON。我想这不是一个可怕的解决方案。
尝试这个:
%dw 2.0
output application/dw
var xml = "<test>XML</test>"
var json = '{"test":1, "test2": 2}'
var test = xml
---
// Try to read it as a JSON
dw::Runtime::try(() -> read(test,"application/json"))
// Now try to read it as an XML
dw::Runtime::orElseTry(() -> read(test,"application/xml"))
// You have data that are neither of XML or JSON
dw::Runtime::orElse("Not XML or JSON")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
470 次 |
| 最近记录: |