无法从 START_ARRAY 令牌反序列化 java.util.HashMap 的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36519974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:57:38  来源:igfitidea点击:

Can not deserialize instance of java.util.HashMap out of START_ARRAY token

javajsonHymanson

提问by Ashish

I am facing issue while parsing JSON using Hymanson-core-2.7.3.jar You can get them from here http://repo1.maven.org/maven2/com/fasterxml/Hymanson/core/

我在使用 Hymanson-core-2.7.3.jar 解析 JSON 时遇到问题您可以从这里获取它们http://repo1.maven.org/maven2/com/fasterxml/Hymanson/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.Hymanson.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.Hymanson.databind.JsonMappingException.from(JsonMappingException.java:216)
at com.fasterxml.Hymanson.databind.DeserializationContext.mappingException(DeserializationContext.java:873)
at com.fasterxml.Hymanson.databind.DeserializationContext.mappingException(DeserializationContext.java:869)
at com.fasterxml.Hymanson.databind.deser.std.StdDeserializer._deserializeFromEmpty(StdDeserializer.java:874)
at com.fasterxml.Hymanson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:337)
at com.fasterxml.Hymanson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
at com.fasterxml.Hymanson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
at com.fasterxml.Hymanson.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 JSONmentioned here is different a JSONwithout Key, for the first element it has value directly and inside that value it has key:valuepair. I am not sure how do I access key:valuepair which is inside a value.

注意:这里JSON提到的是不同的 a JSONwithout Key,对于第一个元素,它直接具有值,并且在该值内它具有key:value对。我不确定如何访问key:value值内的对。

采纳答案by Vikrant Kashyap

Create a simple pojoClass 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));

myObjectswill contains the Listof MyClass. Now you can access this list as per your requirement.

myObjects将包含ListMyClass。现在,您可以根据需要访问此列表。

回答by KrishnaSingh

It seem's that your file is representing a List of objects with Nameand CreationDatefields.

您的文件似乎表示具有NameCreationDate字段的对象列表。

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);

回答by freedev

This exception is raised because you're trying to deserialize a Listin a Map.

引发此异常是因为您试图反序列化 aList中的 a Map

The solution is create a TypeReference of List<Map<String, Object>>:

解决方案是创建一个 TypeReference List<Map<String, Object>>

List<Map<String, Object>> myObjects = 
          mapper.readValue(mapData , new TypeReference<List<Map<String, Object>>>(){});

回答by Guru

Well, you are trying to convert an json stringthat contains array/list of objectsinto 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;)

简单的解决方法是从字符串中删除第一个和最后一个字符并尝试。希望能帮助到你;)

回答by alexandrubarbat

Why not?

为什么不?

ObjectMapper mapper = new ObjectMapper();

Object obj = mapper.readValue(file, new TypeReference<Object>() {});