jacson解析json串value带反斜杠报错

String string="{\"deviceNo\":\"c:\\a\"};

Mybean mybean= (Mybean )JsonMapper.getInstance().fromJson(string, Mybean .class);


报错如下:

com.fasterxml.jackson.databind.JsonMappingException: Unrecognized character escape 'a' (code 77)
 at [Source: java.io.StringReader@259f1b1d; line: 1, column: 734] (through reference chain: com.taiji.kafka2stormunite.util.KakouLdVehiclePassEntity["imageDescriptions"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1311)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:297)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
at com.taiji.kafka2stormunite.util.JsonMapper.fromJson(JsonMapper.java:105)
at com.taiji.kafka2stormunite.util.JsonMapper.main(JsonMapper.java:221)
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'M' (code 77)
 at [Source: java.io.StringReader@259f1b1d; line: 1, column: 734]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1369)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:532)
at com.fasterxml.jackson.core.base.ParserMinimalBase._handleUnrecognizedCharacterEscape(ParserMinimalBase.java:507)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._decodeEscaped(ReaderBasedJsonParser.java:1788)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._finishString2(ReaderBasedJsonParser.java:1535)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._finishString(ReaderBasedJsonParser.java:1512)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.getText(ReaderBasedJsonParser.java:205)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:59)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.mapObject(UntypedObjectDeserializer.java:211)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:51)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.mapObject(UntypedObjectDeserializer.java:225)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer.deserialize(UntypedObjectDeserializer.java:51)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:230)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:207)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:1)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
... 5 more
打断点看了下,取value的时候只取了c:,可见遇到反斜杠终止去字符填充到value,后面的无法解析报错。

com.fasterxml.jackson.core.JsonParser这个类设置了json解析属性。

        /**
         * Feature that can be enabled to accept quoting of all character
         * using backslash qooting mechanism: if not enabled, only characters
         * that are explicitly listed by JSON specification can be thus
         * escaped (see JSON spec for small list of these characters)
         *


         * Since JSON specification requires quoting for all control characters,
         * this is a non-standard feature, and as such disabled by default.
         */
        ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER(false),
允许接受引号引起来的所有字符,默认为false

设置为true问题解决。

你可能感兴趣的:(java,jacson,反斜杠)