我想将XML转换为JSON,这是我的详细信息和要求。
我的XMl以下:
<Message>
<OrderList>
<Order>
<PaymentMethods>
<PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="1111" CreditCardType="VISA" AmountToRefund="10" />
<PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="2222" CreditCardType="MASTER" AmountToRefund="20" />
</PaymentMethods>
</Order>
<Order>
<PaymentMethods>
<PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="1111" CreditCardType="VISA" AmountToRefund="10" />
<PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="2222" CreditCardType="MASTER" AmountToRefund="20" />
<PaymentMethod FirstName="Avnish" LastName="Singh" PaymentType="Card" CreditCardNo="3333" CreditCardType="VISA" AmountToRefund="12" />
</PaymentMethods>
</Order>
</OrderList>
</Message>
我在下面使用groupBy尝试过,并在下面的代码中进行求和,但未获得预期的输出,如下所示:
{
"template_attributes": {
"payment_methods": payload.Message.OrderList..*PaymentMethod groupBy ($.CreditCardType ++ $.CreditCardNo) map ((paymentMethod, index) -> {
"first_name": paymentMethod.@FirstName,
"last_name": paymentMethod.@LastName,
"card_type": paymentMethod.@CreditCardType,
"card_no": paymentMethod.@CreditCardNo,
"amount_to_refund": paymentMethod.@AmountToRefund,
"payment_type": sum paymentMethod.@PaymentType
})
}
}
我要在“输出”下面,在该处应根据card_type和credit_card_no组合所有付款方式:
template_attributes:{
payment_methods:[
{
first_name: "Avnish",
last_name: "Singh",
credit_card_no: "1111",
card_type: "VISA",
amount: "20"
},
{
first_name: "Avnish",
last_name: "Singh",
credit_card_no: "2222",
card_type: "MASTER",
amount: "40"
},
{
first_name: "Avnish",
last_name: "Singh",
credit_card_no: "3333",
card_type: "VISA",
amount: "12"
}
]
}
答案 0 :(得分:1)
嗨,我认为您使用的是m子3.x和dw 1.0。然后解决您的问题。使用pluck运算符对对象进行处理,就好像它是键值对数组一样。
{
"template_attributes": {
"payment_methods":
payload.Message.OrderList..*PaymentMethod
groupBy ((value) -> value.@CreditCardType ++ value.@CreditCardNo)
pluck ((paymentMethod, key) -> {
"first_name": paymentMethod[0].@FirstName,
"last_name": paymentMethod[0].@LastName,
"card_type": paymentMethod[0].@CreditCardType,
"card_no": paymentMethod[0].@CreditCardNo,
"amount_to_refund": sum paymentMethod.@AmountToRefund,
"payment_type": paymentMethod[0].@PaymentType
})
}
}