无法从START_ARRAY标记中反序列化java.util.HashMap的实例 [英] Can not deserialize instance of java.util.HashMap out of START_ARRAY token
问题描述
我在使用jackson-core-2.7.3.jar
解析JSON时遇到问题你可以从这里得到它们 http://repo1.maven.org/maven2/com/fasterxml/jackson/core/
I am facing issue while parsing JSON using jackson-core-2.7.3.jar You can get them from here http://repo1.maven.org/maven2/com/fasterxml/jackson/core/
我的JSON文件是
[
{
"Name": "System Idle Process",
"CreationDate": "20160409121836.675345+330"
},
{
"Name": "System",
"CreationDate": "20160409121836.675345+330"
},
{
"Name": "smss.exe",
"CreationDate": "20160409121836.684966+330"
}
]
我试图解析的Java代码是
and the Java Code is by which I am trying to parse this is
byte[] mapData = Files.readAllBytes(Paths.get("process.txt"));
Map<String,String> myMap = new HashMap<String, String>();
ObjectMapper objectMapper=new ObjectMapper();
myMap = objectMapper.readValue(mapData, HashMap.class);
System.out.println("Map is: "+myMap);
但在执行时我收到错误
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.HashMap out of START_ARRAY token
at [Source: [B@34ce8af7; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:216)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:873)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:869)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromEmpty(StdDeserializer.java:874)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:337)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2872)
我试过在stackoverflow上搜索但是找不到这种类型的JSON的匹配解决方案。
I have tried searching over stackoverflow but couldnot find a matchable solution to this type of JSON.
任何帮助我将不胜感激。
Any help would be appreciated.
注意:这里提到的
JSON
与JSON
没有Key
,对于第一个元素,它直接有值,并且在该值内有键:值
对。
我不知道如何访问key:value
值在一个值内。
NOTE: This
JSON
mentioned here is different aJSON
withoutKey
, for the first element it has value directly and inside that value it haskey:value
pair. I am not sure how do I accesskey:value
pair which is inside a value.
推荐答案
创建一个简单的 pojo
Class First
Create a simple pojo
Class First
class MyClass
{
@JsonProperty
private String Name;
@JsonProperty
private String CreationDate;
}
并使用此代码......
and use this code...
byte[] mapData = Files.readAllBytes(Paths.get("process.txt"));
ObjectMapper objectMapper=new ObjectMapper();
//add this line
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<MyClass> myObjects = mapper.readValue(mapData , new TypeReference<List<MyClass>>(){});
或
byte[] mapData = Files.readAllBytes(Paths.get("process.txt"));
ObjectMapper objectMapper=new ObjectMapper();
//add this line
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<MyClass> myObjects = mapper.readValue(mapData , mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
myObjects
将包含
。现在,您可以根据自己的要求访问此列表。列表
MyClass
myObjects
will contains the List
of MyClass
. Now you can access this list as per your requirement.
这篇关于无法从START_ARRAY标记中反序列化java.util.HashMap的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!