Unable to deserialize java.util.HashMap instance from START_ARRAY token

I ran into a problem 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

[
    {
        "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 with which I am trying to parse this,

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 after execution I get an error message

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 tried to search on stackoverflow, but could not find a suitable solution for this type of JSON.

Any help would be appreciated.

NOTE. This one JSON, mentioned here, is different from JSONwithout Key, for the first element it has a value directly and inside this value has a pair key:value. I'm not sure how I can access a pair key:valuethat is inside the value.

+10
5

pojo First

class MyClass
{
@JsonProperty
private String Name;
@JsonProperty
private String CreationDate;
}

...

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 List of MyClass. .

+7

, CreationDate.

, List HashMap ObjectMapper, :

List<HashMap> dataAsMap = objectMapper.readValue(mapData, List.class);
+6

, List Map.

TypeReference List<Map<String, Object>>:

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

Well, you are trying to convert a json string that contains an array / list of objects to an object .

Example: { "key": "value"}- this is the json you want to convert, but actually you do,

[{ "key": "value"}].

a simple fix is ​​to remove the first and last character from your string and try. Hope it helps;)

0
source

Why not?

ObjectMapper mapper = new ObjectMapper();

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

All Articles