我想将 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"
}
]
}