1. 程式人生 > >json通過實體類轉換時出現的異常

json通過實體類轉換時出現的異常

1、空值轉換-異常情況:
[html] view plain copy
Can not instantiate value of type [map type; class java.util.HashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] from JSON String; no single-String constructor/factory method
解決方法,在ObjectMapper配置中增加:
[html] view plain copy
mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) ;

2、轉義字元-異常情況:
[html] view plain copy
org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 9)): has to be escaped using backslash to be included in string value
at [Source: [email protected]; line: 1, column: 2461]
解決辦法,在ObjectMapper配置中增加:
[html] view plain copy
mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;