无法在 Mule 负载上使用 Groovy 迭代 JSON 地图

Unable to iterate JSON map using Groovy on Mule payload

我在 Groovy 脚本中迭代 JSON(地图)时遇到问题,这是我的问题的详细信息。

在 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 并使用 return class java.util.Map 对对象执行 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 脚本中进行迭代吗?

感谢您的帮助

您没有正确访问产品,是 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()
}

根据你的JSON结构,应该是:

xml.Products(){
                for(Product in payload.order.Products){
                        xml.Product(){
                            Quantity(Product.Product.Quantity)
                            UnitPrice(Product.Product.UnitPrice)
                            Code(Product.Product.Code)
                        }

                }
            }