org.codehaus.jackson.JsonParseException 反序列化字符串时
org.codehaus.jackson.JsonParseException while deserialising String
我在资源文件中有以下字符串,我正在尝试使用 ObjectMapper
反序列化为 Map
{ 13714974: { get-url: "https://example.com/get", post-url:
"https://example.com/post" }, 13743772: { get-url:
"https://example.com/get", post-url: "https://example.com/post" }
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
Map<String, Map<String, String>> cmMappings = mapper.readValue(content, Map.class);
当我使用 codehaus jackson ObjectMapper 并尝试反序列化它时,出现异常,
Exception in thread "main" org.codehaus.jackson.JsonParseException: Unexpected character ('1' (code 49)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name
at [Source: java.io.StringReader@7cdbc5d3; line: 1, column: 3]
at org.codehaus.jackson.impl.JsonParserBase._constructError(JsonParserBase.java:651)
at org.codehaus.jackson.impl.JsonParserBase._reportError(JsonParserBase.java:635)
at org.codehaus.jackson.impl.JsonParserBase._reportUnexpectedChar(JsonParserBase.java:576)
at org.codehaus.jackson.impl.ReaderBasedParser._handleUnusualFieldName(ReaderBasedParser.java:385)
at org.codehaus.jackson.impl.ReaderBasedParser._parseFieldName(ReaderBasedParser.java:268)
at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:118)
当 运行 反序列化包含这种格式字符串的资源文件的代码时,我得到 Exception while deserialising because of hyphen in field names
。
我也尝试了 fasterxml ObjectMapper,但它不起作用
那么如何将字符串调试到映射?
这就是我们反序列化上述字符串的方式
try {
ObjectMapper mapper = new ObjectMapper();
ClassPathResource resource = new ClassPathResource("resourceFile");
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
TypeReference<HashMap<String, Map<String, String>>> typeRef = new TypeReference<HashMap<String, Map<String, String>>>() {};
cmMappings = mapper.readValue(resource.getInputStream(), typeRef);
} catch (Exception ex) {
// log error
}
我在资源文件中有以下字符串,我正在尝试使用 ObjectMapper
反序列化为 Map{ 13714974: { get-url: "https://example.com/get", post-url: "https://example.com/post" }, 13743772: { get-url: "https://example.com/get", post-url: "https://example.com/post" }
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
Map<String, Map<String, String>> cmMappings = mapper.readValue(content, Map.class);
当我使用 codehaus jackson ObjectMapper 并尝试反序列化它时,出现异常,
Exception in thread "main" org.codehaus.jackson.JsonParseException: Unexpected character ('1' (code 49)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name
at [Source: java.io.StringReader@7cdbc5d3; line: 1, column: 3]
at org.codehaus.jackson.impl.JsonParserBase._constructError(JsonParserBase.java:651)
at org.codehaus.jackson.impl.JsonParserBase._reportError(JsonParserBase.java:635)
at org.codehaus.jackson.impl.JsonParserBase._reportUnexpectedChar(JsonParserBase.java:576)
at org.codehaus.jackson.impl.ReaderBasedParser._handleUnusualFieldName(ReaderBasedParser.java:385)
at org.codehaus.jackson.impl.ReaderBasedParser._parseFieldName(ReaderBasedParser.java:268)
at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:118)
当 运行 反序列化包含这种格式字符串的资源文件的代码时,我得到 Exception while deserialising because of hyphen in field names
。
我也尝试了 fasterxml ObjectMapper,但它不起作用
那么如何将字符串调试到映射?
这就是我们反序列化上述字符串的方式
try {
ObjectMapper mapper = new ObjectMapper();
ClassPathResource resource = new ClassPathResource("resourceFile");
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
TypeReference<HashMap<String, Map<String, String>>> typeRef = new TypeReference<HashMap<String, Map<String, String>>>() {};
cmMappings = mapper.readValue(resource.getInputStream(), typeRef);
} catch (Exception ex) {
// log error
}