如何用 mule 读取 json 文件?

How read a json file with mule?

我的源代码中有这个部分:

<file:inbound-endpoint connector-ref="fileConnector" path="C:/tmp/input" encoding="UTF-8" mimeType="application/json">
       <file:filename-wildcard-filter pattern="*.json"/>
</file:inbound-endpoint>

<json:json-to-object-transformer returnClass="java.util.HashMap"/>

<logger level="INFO" message="#[message.payload]"/>

编译一切正常!但在执行时抛出下一个异常:"Message: failed to transform from "json" to "java.util.HashMap" Code: MULE-ERROR-109"

谁能帮我解决这个错误?

流程如下:

<file:inbound-endpoint 
  connector-ref="fileConnector" path="C:/tmp/input" 
  doc:name="File Input" responseTimeout="10000" 
  encoding="UTF-8" mimeType="text/plain">
  <file:filename-wildcard-filter pattern="*.json"/>
</file:inbound-endpoint>

<json:json-to-object-transformer returnClass="java.util.HashMap"doc:name="JSON to Object"/>`

The error was in the JSON file. The character encoding was not right and for this reason I was generating errors when trying to transform through <json: json-to-object-transformer returnClass = "java.util.HashMap" doc: name = "JSON to Object" / > after making reading the file with <file: inbound-endpoint> attribute where I had defined as encoding = "UTF-8"

解决方案

Change the character encoding of the JSON file to "UTF-8"

知识库:

If the exception stack is:

Message: Failed to transform from "json" to "java.util.HashMap" Code: MULE_ERROR-109


Exception stack is:

  1. Unexpected character ((code 65279 / 0xFEFF) '?'): Expected to valid value (number, string, array, object, 'true', 'false' or 'null')  at [Source: java.io.InputStreamReader@2fba237; line 1, column 2 (org.codehaus.jackson.JsonParseException) org.codehaus.jackson.JsonParser: 1433 (null)
  2. Failed to transform from "json" to "java.util.HashMap (org.mule.api.transformer.TransformerException) org.mule.module.json.transformers.JsonToObject: 132 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)

Consider change the character encoding of the JSON file