我创建了一个显示错误的简单测试用例。如果我错过了什么,请告诉我。
我有一个声明单个访问器的简单接口:
public class JacksonParsingTest {
public interface EventWithMap {
Map<String, String> getContextMap();
}
@Test
public void testJsonMapDeserializes()
throws JsonParseException, JsonMappingException, IOException
{
String json = "{\"contextMap\":[" +
"{\"key\":\"processedAt\",\"value\":\"Thu Dec 31 14:30:51 EST 2015\"}" +
"]}";
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule( new MrBeanModule() );
EventWithMap e = mapper.readValue( json, EventWithMap.class );
assertEquals( "Thu Dec 31 14:30:51 EST 2015", e.getContextMap().get( "processedAt" ) );
}
}
当我运行这个单元测试时,Jackson 无法解析 json 字符串:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
at [Source: {"contextMap":[{"key":"processedAt","value":"Thu Dec 31 14:30:51 EST 2015"}]}; line: 1, column: 2] (through reference chain: RecoveryEventBuilderTest$EventWithMap["contextMap"])
正如您从堆栈跟踪中看到的那样,Jackson 正确地从 json 中提取了 contextMap 标记,但它无法继续标记字符串。据我所知,json字符串正确地代表了一张地图。
谢谢,罗宾。