[英]Can not deserialize instance of java.util.HashMap out of START_ARRAY token
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/我在使用 jackson-core-2.7.3.jar 解析 JSON 时遇到问题您可以从这里获取它们http://repo1.maven.org/maven2/com/fasterxml/jackson/core/
My JSON File is我的 JSON 文件是
[
{
"Name": "System Idle Process",
"CreationDate": "20160409121836.675345+330"
},
{
"Name": "System",
"CreationDate": "20160409121836.675345+330"
},
{
"Name": "smss.exe",
"CreationDate": "20160409121836.684966+330"
}
]
and the Java Code is by which I am trying to parse this is我试图解析的Java代码是
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);
But upon execution I am getting the error但是在执行时我收到错误
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)
I have tried searching over stackoverflow but couldnot find a matchable solution to this type of JSON.我曾尝试在 stackoverflow 上进行搜索,但找不到与此类 JSON 匹配的解决方案。
Any help would be appreciated.任何帮助将不胜感激。
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.注:此
JSON
这里提到的是不同JSON
没有Key
,因为它直接具有价值的第一要素和价值里面有key:value
对。 I am not sure how do I accesskey:value
pair which is inside a value.我不确定如何访问
key:value
内的key:value
对。
This exception is raised because you're trying to deserialize a List
in a Map
.引发此异常是因为您试图反序列化
Map
的List
。
The solution is create a TypeReference of List<Map<String, Object>>
:解决方案是创建一个
List<Map<String, Object>>
的 TypeReference :
List<Map<String, Object>> myObjects =
mapper.readValue(mapData , new TypeReference<List<Map<String, Object>>>(){});
Create a simple pojo
Class First首先创建一个简单的
pojo
类
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>>(){});
or或
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
will contains the List
of MyClass
. myObjects
将包含MyClass
List
。 Now you can access this list as per your requirement.现在,您可以根据需要访问此列表。
It seem's that your file is representing a List of objects with Name and CreationDate fields.您的文件似乎表示具有Name和CreationDate字段的对象列表。
So you have to just use List instead of HashMap to ObjectMapper, code is mentioned below:所以你只需要使用 List 而不是 HashMap 到 ObjectMapper,代码如下:
List<HashMap> dataAsMap = objectMapper.readValue(mapData, List.class);
Well, you are trying to convert an json string that contains array/list of objects into an object .好吧,您正在尝试将包含对象数组/列表的json 字符串转换为object 。
Ex: { "key": "value"}
is the json you want to convert, but actually you are doing,例如:
{ "key": "value"}
是您要转换的 json,但实际上您正在这样做,
[{ "key": "value"}]
. [{ "key": "value"}]
。
simple fix, is remove the first and last char from your string and try.简单的解决方法是从字符串中删除第一个和最后一个字符并尝试。 Hope it helps;)
希望它有帮助;)
Why not?为什么不呢?
ObjectMapper mapper = new ObjectMapper();
Object obj = mapper.readValue(file, new TypeReference<Object>() {});
Your JSON is not a Map, but a List of Maps.您的 JSON 不是地图,而是地图列表。 So replace this:
所以替换这个:
myMap = objectMapper.readValue(mapData, HashMap.class);
with this:有了这个:
myMap = objectMapper.readValue(mapData, new TypeReference<List<Map<String, String>>>(){});
Or you can change the format of your JSON data...或者您可以更改 JSON 数据的格式...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.