Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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/

My JSON File is

[     {         "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

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.

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:valuepair. I am not sure how do I access key:value pair which is inside a value.

like image 868
Ashish Avatar asked Apr 09 '16 16:04

Ashish


2 Answers

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

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

List<Map<String, Object>> myObjects =            mapper.readValue(mapData , new TypeReference<List<Map<String, Object>>>(){}); 
like image 149
freedev Avatar answered Sep 18 '22 11:09

freedev


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>>(){}); 

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. Now you can access this list as per your requirement.

like image 22
Vikrant Kashyap Avatar answered Sep 17 '22 11:09

Vikrant Kashyap