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 aJSON
withoutKey
, for the first element it has value directly and inside that value it haskey:value
pair. I am not sure how do I accesskey:value
pair which is inside a value.
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments