简体   繁体   English

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

[英]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 a JSON without Key , for the first element it has value directly and inside that value it has key:value pair.注:此JSON这里提到的是不同JSON没有Key ,因为它直接具有价值的第一要素和价值里面有key:value对。 I am not sure how do I access key: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 .引发此异常是因为您试图反序列化MapList

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.您的文件似乎表示具有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);

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.

相关问题 JSON到XML转换中的错误:无法从START_ARRAY令牌中反序列化java.util.HashMap的实例 - error in JSON to XML conversion: Can not deserialize instance of java.util.HashMap out of START_ARRAY token 无法将Java文件中的JSON文件转换为hashmap并出现错误,无法从START_ARRAY令牌中反序列化java.util.HashMap的实例 - convert JSON file to a hashmap in java with error Can not deserialize instance of java.util.HashMap out of START_ARRAY token 忽略“无法从START_ARRAY令牌中反序列化java.util.LinkedHashMap的实例”错误 - Ignore the “Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token” error 无法从我的“DataRequests”dto 中的 START_ARRAY 令牌中反序列化 java.util.LinkedHashMap 的实例 - Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token in my 'DataRequests' dto 错误:无法在 [来源:N/A; 行:-1,列:-1] - Error: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token at [Source: N/A; line: -1, column: -1] 使用Jackson反序列化JSON时出错:无法从START_ARRAY令牌中反序列化java.util.LinkedHashMap实例 - Error while deserializing JSON using Jackson : Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token 无法使用START_ARRAY令牌反序列化对象实例 - Can not deserialize instance of object out of START_ARRAY token 无法反序列化实例,超出 START_ARRAY 令牌 - Can not deserialize instance, out of START_ARRAY token 无法从START_ARRAY标记中反序列化Task的实例 - Can not deserialize instance of Task out of START_ARRAY token 无法从START_ARRAY令牌中反序列化JsonGen实例:newbee - Can not deserialize instance of JsonGen out of START_ARRAY token : newbee
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM