JsonParseException,不正确的字符不在JSON中

时间:2014-08-15 21:53:37

标签: java json jackson

我正在尝试在Java中处理以下格式的JSON。

    String flowJSON = "{\"FlowDTO\":[{\"policyName\":\"Firewall\",\"action\":\"DROP\",\"sourceIp\":\"ANY\",\"destinationIp\":\"ANY\",\"datapathId\":\"8506829779379520\",\"sourcePort\":\"ANY\",\"destinationPort\":\"78\",\"protocol\":\"TCP\",\"priority\":\"0\",\"rateLimiter\":\10000\"}]}";

JsonNode root = mapper.readTree(flowJSON); 

对于上面的代码,我收到以下错误。但是,当我在JSON中没有该字符时,我不明白为什么不正确的字符是'@'。我没有正确阅读此错误吗?

[2014-08-15 14:44:37.095] ERROR POLLtimer                    System.err                                                        com.fasterxml.jackson.core.JsonParseException: Unexpected character ('@' (code 64)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') 
[2014-08-15 14:44:37.095] ERROR POLLtimer                    System.err                                                         at [Source: java.io.StringReader@4e511927; line: 1, column: 214] 
[2014-08-15 14:44:37.096] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1378) 
[2014-08-15 14:44:37.096] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:599) 
[2014-08-15 14:44:37.096] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:520) 
[2014-08-15 14:44:37.097] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleUnexpectedValue(ReaderBasedJsonParser.java:1387) 
[2014-08-15 14:44:37.097] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:678) 
[2014-08-15 14:44:37.097] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject(JsonNodeDeserializer.java:194) 
[2014-08-15 14:44:37.098] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeArray(JsonNodeDeserializer.java:230) 
[2014-08-15 14:44:37.098] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject(JsonNodeDeserializer.java:202) 
[2014-08-15 14:44:37.099] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:58) 
[2014-08-15 14:44:37.099] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:15) 
[2014-08-15 14:44:37.099] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2793) 
[2014-08-15 14:44:37.100] ERROR POLLtimer                    System.err                                                         at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:1659) 
[2014-08-15 14:44:37.100] ERROR POLLtimer                    System.err                                                         at com.hp.sf.impl.fucms.PollFlowTimerTask.run(PollFlowTimerTask.java:95) 
[2014-08-15 14:44:37.101] ERROR POLLtimer                    System.err                                                         at java.util.TimerThread.mainLoop(Timer.java:555) 
[2014-08-15 14:44:37.102] ERROR POLLtimer                    System.err                                                         at java.util.TimerThread.run(Timer.java:505) 

2 个答案:

答案 0 :(得分:3)

您在这里错过了双引号:\"rateLimiter\":\10000\"

\100被视为以八进制表示的单个字符。如果你看一个ascii表,你会看到字符64(八进制100)是一个@符号。因此,您的字符串包含"rateLimiter":@00"而不是"rateLimiter":"10000"

答案 1 :(得分:1)

字符串中的最后一个数字缺少引号:

\10000\"

\"10000\"

将来可以使用http://jsonlint.com/进行JSON验证。