简体   繁体   中英

error in JSON to XML conversion: Can not deserialize instance of java.util.HashMap out of START_ARRAY token

I'm completely new to this thing, I'm trying to convert JSON to XML in groovyscript, here's the code:

import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil

def xmlBuilder = new StreamingMarkupBuilder()
def item = payload
writer = xmlBuilder.bind {
mkp.declareNamespace( ns1: "TV")

  'ns1:tranfer ' {
    'merchant_id'()
    'surveyYear'()
    'merchant_password' ()
    'email'(item.AdditionalInfo.ClientEmail)
    'amount'(item.Amount)
    'reference_code'(item.WithdrawalId)
  }
}

return XmlUtil.serialize( writer )

I'm using anypoint studio and SOAP UI for testing.

When I use a single JSON element like this, it works

{ 
           "WithdrawalId": 12345, 
           "LegalEntity" : "xkvt", 
           "ProviderReference": "123xyz", 
           "Currency": "USD", 
           "Amount": 77, 
           "AdditionalInfo": {"ClientEmail":"test@test.com","CustomFieldX":"x"} 
        } 

But when I add another element to JSON, it is unable to "deserialize"

[
  { 
           "WithdrawalId": 12345, 
           "LegalEntity" : "xkvt", 
           "ProviderReference": "123xyz", 
           "Currency": "USD", 
           "Amount": 757, 
           "AdditionalInfo": {"ClientEmail" : "tony@gmail.com"} },

{


           "WithdrawalId": 12345, 
           "LegalEntity" : "xkvt2", 
           "ProviderReference": "123xyz", 
           "Currency": "USD", 
           "Amount": 7808, 
           "AdditionalInfo": {"ClientEmail" : "tony@gmail.com"} } 

]

Here's the exact error thrown on SOAPui:

"<data contentType="null" contentLength="113"><![CDATA[Failed to transform        from "json" to "java.util.HashMap". (org.mule.api.transformer.TransformerMessagingException).]]></data>"

Any ideas how I can solve this? Thanks!

How is your mapper configured. Try including the line below in your mapper configuraiton - mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM