我正在获取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中提取该数组。提前谢谢
答案 0 :(得分:0)
您可以使用 Dataweave (在Anypoint Studio中转换消息组件)
(Mule EE)
查看文档:
https://docs.mulesoft.com/mule-user-guide/v/3.7/using-dataweave-in-studio
输入的示例脚本:
public void button1_Click(object sender, EventArgs e)
{
SqlCommand cm = con.CreateCommand();
cm.CommandText = "Select * from H_Facturi_Clienti";
try
{
SqlDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListViewItem item = new ListViewItem(dr["H_Id"].ToString());
item.SubItems.Add(dr["Serie"].ToString());
item.SubItems.Add(dr["Numar"].ToString());
item.SubItems.Add(dr["Id_Partener"].ToString());
item.SubItems.Add(dr["Data"].ToString());
item.SubItems.Add(dr["Valoare"].ToString());
listView1.Items.Add(item);
}
}
catch (Exception)
{
statusLbl.Text = "Eroare ";
}
}
使用%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
答案 1 :(得分:-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"/>