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
JSONmentioned here is different aJSONwithoutKey, for the first element it has value directly and inside that value it haskey:valuepair. I am not sure how do I accesskey:valuepair which is inside a value.
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>>>(){});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With