我在Groovy脚本中迭代JSON(map)时遇到了麻烦,这里是我问题的详细信息。
在Mule我收到一个JSON。这是:
{
"order": {
"ContactId": "112",
"Discount": "0.000000",
"OrderDate": "2015-03-26 15:26:38",
"OrderNumber": "VBOKLZZZF",
"Total": "43.810000",
"NumberOfChild": "2",
"PaymentMethod": 1,
"SpouseName": "Firstname Lastname",
"Products": [
{
"Product": {
"Quantity": "1",
"UnitPrice": null,
"Code": "AB20"
}
},
{
"Product": {
"Quantity": "1",
"UnitPrice": null,
"Code": "AB20"
}
}
]
}
}
我想使用Groovy的MarkupBuilder转换为XML。这是我正在寻找的输出:
<SaveOrder xmlns='http://tempuri.org/'>
<order xmlns='http://schemas.datacontract.org/2004/07/Service.Entities'>
<ContactId>112</ContactId>
<Discount>0.000000</Discount>
<OrderDate>2015-03-26 15:26:38</OrderDate>
<OrderNumber>VBOKLZZZF</OrderNumber>
<Total>43.810000</Total>
<NumberOfChild>2</NumberOfChild>
<PaymentMethod>1</PaymentMethod>
<SpouseName>FirstName Lastname</SpouseName>
<Products xmlns:"http://schemas.datacontract.org/2004/07/Service.Entities">
<ns1:Product>
<ns1:Code>AB20</ns1:Code>
<ns1:Quantity>1</ns1:Quantity>
</ns1:Product>
<ns1:Product>
<ns1:Code>AB20</ns1:Code>
<ns1:Quantity>1</ns1:Quantity>
</ns1:Product>
</ns1:Products>
</order>
</SaveOrder>
在Mule中,我收到JSON并使用返回类java.util.Map对Object执行JSON。
这是我的Groovy脚本代码。
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
xml.SaveOrder(xmlns: 'http://tempuri.org/') {
xml.order(xmlns: 'http://schemas.datacontract.org/2004/07/Service.Entities'){
ContactId(payload.order.ContactId)
Discount(payload.order.Discount)
OrderDate(payload.order.OrderDate)
OrderNumber(payload.order.OrderNumber)
Total(payload.order.Total)
NumberOfChild(payload.order.NumberOfChild)
PaymentMethod(payload.order.PaymentMethod)
SpouseName(payload.order.SpouseName)
xml.Products(){
for(Product in payload.Products){
xml.Product(){
Quantity(Product.Quantity)
UnitPrice(Product.UnitPrice)
Code(Product.Code)
}
}
}
}
}
result = writer.toString()
但我只能得到这个
<SaveOrder xmlns='http://tempuri.org/'>
<order xmlns='http://schemas.datacontract.org/2004/07/Service.Entities'>
<ContactId>112</ContactPrestashopId>
<Discount>0.000000</Discount>
<OrderDate>2015-03-26 15:26:38</OrderDate>
<OrderNumber>VBOKLZZZF</OrderNumber>
<Total>43.810000</Total>
<NumberOfChild>2</NumberOfChild>
<PaymentMethod>1</PaymentMethod>
<SpouseName>Firstname Lastname</SpouseName>
<Products />
</order>
</SaveOrder>
有人可以帮我解决Groovy脚本中的迭代问题吗?
感谢您的帮助
答案 0 :(得分:1)
您没有正确使用产品,而是payload.order.Products
;
def xml = new StringWriter().with { w ->
new MarkupBuilder(w).with {
SaveOrder(xmlns: 'http://tempuri.org/') {
order(xmlns: 'http://schemas.datacontract.org/2004/07/Service.Entities') {
ContactId(payload.order.ContactId)
Discount(payload.order.Discount)
OrderDate(payload.order.OrderDate)
OrderNumber(payload.order.OrderNumber)
Total(payload.order.Total)
NumberOfChild(payload.order.NumberOfChild)
PaymentMethod(payload.order.PaymentMethod)
SpouseName(payload.order.SpouseName)
Products(xmlns:"http://schemas.datacontract.org/2004/07/Service.Entities") {
payload.order.Products.each { p ->
'ns1:Product' {
'ns1:Code'(p.Product.Code)
'ns1:Quantity'(p.Product.Quantity)
}
}
}
}
}
}
w.toString()
}
答案 1 :(得分:1)
根据您的JSON结构,它应该是:
xml.Products(){
for(Product in payload.order.Products){
xml.Product(){
Quantity(Product.Product.Quantity)
UnitPrice(Product.Product.UnitPrice)
Code(Product.Product.Code)
}
}
}