我在ESB中获取JSON数组响应,但我需要使用脚本中介将其转换为XML。 JSON数组响应的长度是动态的。我需要迭代整个JSON数组并将其转换为xml。请帮忙。
下面是我现在得到的JSON数组响应,但长度不固定。
{
"Results": [
{
"Id": 215766,
"HostName": "xxx.xxx.xx.xxx",
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP COLOR LASERJET CP4520 SERIES",
"SerialNumber": "JPDXG57J7Y",
"FirmwareVersion": "07.160.6",
"FirmwareDateCode": "2014-01-27T00:00:00Z",
"LastContactTime": "2017-06-19T04:03:04Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "3ff42b50-9d86-cf53-e053-0100007ffbe3",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "GenuineHP",
"HasGenuineHPCartridges": true
},
{
"Id": 215757,
"HostName": "xxx.xxx.xx.xxx",
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP COLOR LASERJET FLOW MFP M880",
"SerialNumber": "CNCXG566XK",
"FirmwareVersion": "2308209_000573",
"FirmwareDateCode": "2016-08-20T00:00:00Z",
"LastContactTime": "2017-06-19T04:02:22Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "3bf42b50-c03b-863a-e053-0100007fad51",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "CartridgeInBadState",
"HasGenuineHPCartridges": true
},
{
"Id": 215763,
"HostName": null,
"IPAddress": "xxx.xxx.xx.xxx",
"ModelName": "HP COLOR LASERJET FLOW MFP M880",
"SerialNumber": "CNDVGCS0LP",
"FirmwareVersion": "2308209_000573",
"FirmwareDateCode": "2016-08-20T00:00:00Z",
"LastContactTime": "2017-06-19T04:03:11Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "3cf42b50-d977-d237-e053-0100007fae25",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "GenuineHP",
"HasGenuineHPCartridges": true
},
{
"Id": 215762,
"HostName": "xxx.xxx.xx.xxx",
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP LASERJET M4345 MFP",
"SerialNumber": "CNCJ94000H",
"FirmwareVersion": "48.306.1",
"FirmwareDateCode": "2014-07-22T00:00:00Z",
"LastContactTime": "2017-06-19T04:05:08Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "3bf42b50-c13b-863a-e053-0100007fad51",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "GenuineHP",
"HasGenuineHPCartridges": true
},
{
"Id": 215759,
"HostName": "xxx.xxx.xx.xxx",
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP LASERJET M4345 MFP",
"SerialNumber": "CNCJ98L01X",
"FirmwareVersion": "48.306.1",
"FirmwareDateCode": "2014-07-22T00:00:00Z",
"LastContactTime": "2017-06-19T04:04:07Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "3df42b50-a14a-a05b-e053-0100007f3f94",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "NotSupported",
"HasGenuineHPCartridges": true
},
{
"Id": 224984,
"HostName": "xxx.xxx.xx.xxx",
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP LASERJET M506",
"SerialNumber": "PHCJQ00233",
"FirmwareVersion": "2308209_000567",
"FirmwareDateCode": "2016-08-20T00:00:00Z",
"LastContactTime": "2017-06-16T04:09:27Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "7aa4ca50-6303-7404-e053-0100007f7e65",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "GenuineHP",
"HasGenuineHPCartridges": true
},
{
"Id": 215758,
"HostName": "xxx.xxx.xx.xxx",
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP LASERJET M9040 MFP",
"SerialNumber": "JPCXDDSBQH",
"FirmwareVersion": "51.320.4",
"FirmwareDateCode": "2017-01-24T00:00:00Z",
"LastContactTime": "2017-06-19T04:02:03Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "3cf42b50-d877-d237-e053-0100007fae25",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "GenuineHP",
"HasGenuineHPCartridges": true
},
{
"Id": 215761,
"HostName": null,
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP LASERJET P4515",
"SerialNumber": "CNFY193829",
"FirmwareVersion": "04.049.3",
"FirmwareDateCode": "2009-09-25T00:00:00Z",
"LastContactTime": "2017-06-14T03:55:39Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "3ff42b50-9b86-cf53-e053-0100007ffbe3",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "GenuineHP",
"HasGenuineHPCartridges": true
},
{
"Id": 240970,
"HostName": "xxx.xxx.xx.xxx",
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP MFP S975DN",
"SerialNumber": "5100008X00",
"FirmwareVersion": "H1.07.t1.00",
"FirmwareDateCode": null,
"LastContactTime": "2017-06-14T05:58:08Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": "xxx",
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "6c8b4351-1f83-b873-e053-0100007ffdc5",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "NotAttempted",
"HasGenuineHPCartridges": true
},
{
"Id": 224986,
"HostName": "xxx.xxx.xx.xxx",
"IPAddress": "xx.xx.xxx.xxx",
"ModelName": "HP PAGEWIDE PRO MFP 777HC",
"SerialNumber": "MY73H5E00H",
"FirmwareVersion": "LIMOFWMP2A006.1720B.00",
"FirmwareDateCode": "2017-05-11T00:00:00Z",
"LastContactTime": "2017-06-14T03:55:40Z",
"TemplateSettings": {
"ConsumablesStrategy": "Unknown",
"LongLifeConsumablesStrategy": "Unknown"
},
"MacAddress": null,
"DeviceIntrinsicAttributeId": "SerialNumber",
"ManufacturerName": "HP",
"DeviceModelSupportTypeId": "Dat",
"IsAmvDevice": false,
"Guid": "7aa4ca50-6503-7404-e053-0100007f7e65",
"MskuAttributeState": "Absent",
"ActivationStatus": "Active",
"GenuineHPCartridgeStatus": "NotAttempted",
"HasGenuineHPCartridges": true
}
]
}
答案 0 :(得分:1)
我通过编写下面的代码来实现它。
<script language="js"><![CDATA[
var payload = mc.getPayloadJSON();
var results = payload.Results;
var response = <locations/>;
for (i = 0; i < results.length; ++i) {
var elem = results[i];
response.locations += <jacdevice>
<JACDeviceId>{elem.Id}</JACDeviceId>
<Make>{elem.ManufacturerName}</Make>
<Model>{elem.ModelName}</Model>
</jacdevice>
}
mc.setPayloadXML(response);
]]></script>
答案 1 :(得分:0)
如果将messageType
属性添加到out序列,它应自动将消息转换为XML。
<outSequence>
<property name="messageType" value="application/xml" scope="axis2"/>
<send/>
</outSequence>
答案 2 :(得分:-1)
我的xml响应应该如下所示
<body>
<p:jacdevice_batch_req xmlns:p="http://ws.wso2.org/dataservice">
<!--1 or more occurrences-->
<p:jacdevice xmlns:p="http://ws.wso2.org/dataservice">
<!--Exactly 1 occurrence-->
<xs:JACDeviceId xmlns:xs="http://ws.wso2.org/dataservice">1</xs:JACDeviceId>
<!--Exactly 1 occurrence-->
<xs:Make xmlns:xs="http://ws.wso2.org/dataservice">HP</xs:Make>
<!--Exactly 1 occurrence-->
<xs:Model xmlns:xs="http://ws.wso2.org/dataservice">HP CM8060 Color MFP</xs:Model>
<!--Exactly 1 occurrence-->
<xs:SerialNumber xmlns:xs="http://ws.wso2.org/dataservice">SerialNumberZ</xs:SerialNumber>
<!--Exactly 1 occurrence-->
<xs:IPv4 xmlns:xs="http://ws.wso2.org/dataservice">IPV4z</xs:IPv4>
<!--Exactly 1 occurrence-->
<xs:MAC xmlns:xs="http://ws.wso2.org/dataservice">MACZZ</xs:MAC>
<!--Exactly 1 occurrence-->
<xs:Hostname xmlns:xs="http://ws.wso2.org/dataservice">HOST NAMEZ</xs:Hostname>
<!--Exactly 1 occurrence-->
<xs:FirstScanDate xmlns:xs="http://ws.wso2.org/dataservice">2017-06-06 10:33:42.773</xs:FirstScanDate>
<!--Exactly 1 occurrence-->
<xs:LastScanDate xmlns:xs="http://ws.wso2.org/dataservice">2017-06-06 10:33:42.773</xs:LastScanDate>
<!--Exactly 1 occurrence-->
<xs:Managed xmlns:xs="http://ws.wso2.org/dataservice">1</xs:Managed>
<!--Exactly 1 occurrence-->
<xs:LastAttemptedContactTime xmlns:xs="http://ws.wso2.org/dataservice">2017-06-06 10:33:42.773</xs:LastAttemptedContactTime>
<!--Exactly 1 occurrence-->
<xs:CredentialProfileStatus xmlns:xs="http://ws.wso2.org/dataservice">aa</xs:CredentialProfileStatus>
<!--Exactly 1 occurrence-->
<xs:JACCredentialProfileId xmlns:xs="http://ws.wso2.org/dataservice">1</xs:JACCredentialProfileId>
<!--Exactly 1 occurrence-->
<xs:FirmwareVersion xmlns:xs="http://ws.wso2.org/dataservice">bb</xs:FirmwareVersion>
<!--Exactly 1 occurrence-->
<xs:FirmwareDateCode xmlns:xs="http://ws.wso2.org/dataservice">cc</xs:FirmwareDateCode>
<!--Exactly 1 occurrence-->
<xs:JACAccountID xmlns:xs="http://ws.wso2.org/dataservice">3113</xs:JACAccountID>
</p:jacdevice>
<p:jacdevice xmlns:p="http://ws.wso2.org/dataservice">
<!--Exactly 1 occurrence-->
<xs:JACDeviceId xmlns:xs="http://ws.wso2.org/dataservice">9</xs:JACDeviceId>
<!--Exactly 1 occurrence-->
<xs:Make xmlns:xs="http://ws.wso2.org/dataservice">HPjj</xs:Make>
<!--Exactly 1 occurrence-->
<xs:Model xmlns:xs="http://ws.wso2.org/dataservice">HP CM8060 Color MFPjj</xs:Model>
<!--Exactly 1 occurrence-->
<xs:SerialNumber xmlns:xs="http://ws.wso2.org/dataservice">SerialNumberkkZ</xs:SerialNumber>
<!--Exactly 1 occurrence-->
<xs:IPv4 xmlns:xs="http://ws.wso2.org/dataservice">IPV4zjj</xs:IPv4>
<!--Exactly 1 occurrence-->
<xs:MAC xmlns:xs="http://ws.wso2.org/dataservice">MACZZjj</xs:MAC>
<!--Exactly 1 occurrence-->
<xs:Hostname xmlns:xs="http://ws.wso2.org/dataservice">HOST NAMEjjZ</xs:Hostname>
<!--Exactly 1 occurrence-->
<xs:FirstScanDate xmlns:xs="http://ws.wso2.org/dataservice">2017-06-06 10:33:42.773</xs:FirstScanDate>
<!--Exactly 1 occurrence-->
<xs:LastScanDate xmlns:xs="http://ws.wso2.org/dataservice">2017-06-06 10:33:42.773</xs:LastScanDate>
<!--Exactly 1 occurrence-->
<xs:Managed xmlns:xs="http://ws.wso2.org/dataservice">1</xs:Managed>
<!--Exactly 1 occurrence-->
<xs:LastAttemptedContactTime xmlns:xs="http://ws.wso2.org/dataservice">2017-06-06 10:33:42.773</xs:LastAttemptedContactTime>
<!--Exactly 1 occurrence-->
<xs:CredentialProfileStatus xmlns:xs="http://ws.wso2.org/dataservice">aa</xs:CredentialProfileStatus>
<!--Exactly 1 occurrence-->
<xs:JACCredentialProfileId xmlns:xs="http://ws.wso2.org/dataservice">1</xs:JACCredentialProfileId>
<!--Exactly 1 occurrence-->
<xs:FirmwareVersion xmlns:xs="http://ws.wso2.org/dataservice">bb</xs:FirmwareVersion>
<!--Exactly 1 occurrence-->
<xs:FirmwareDateCode xmlns:xs="http://ws.wso2.org/dataservice">cc</xs:FirmwareDateCode>
<!--Exactly 1 occurrence-->
<xs:JACAccountID xmlns:xs="http://ws.wso2.org/dataservice">3113</xs:JACAccountID>
</p:jacdevice>
</p:jacdevice_batch_req>
</body>