Reputation: 251
I am trying to convert my json into a map uisng jackson-core-2.9.6 . My json is
{
"name": "Loren",
"inputDetails": {
"truncated": false,
"result": [
{
"subjects": [
{
"math": "50",
"SST": "37 ",
"status": "ACTIVE"
}
]
}
]
},
"timeoutInSeconds": null
}
and the Java Code is by which I am trying to parse this JSON into Map is like this
public static void main(String[]args){
String request="{\r\n \"name\": \"Loren\",\r\n \"inputDetails\": {\r\n \"truncated\": false,\r\n \"result\": [\r\n {\r\n \"subjects\": [\r\n {\r\n \"math\": \"50\",\r\n \"SST\": \"37 \",\r\n \"status\": \"ACTIVE\"\r\n }\r\n ]\r\n }\r\n ]\r\n },\r\n \"timeoutInSeconds\": null\r\n}";
try {
List servicesResponse = null;
Map<String,Object> inputParam=null;
ObjectMapper objectMapper = new ObjectMapper();
request = objectMapper.writeValueAsString(request);
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
inputParam = (Map<String, Object>) objectMapper.readValue(request, Map.class);
servicesResponse = (List)inputParam.get("result");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Exception which I am getting is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of java.util.LinkedHashMap
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{
"name": "Loren",
"inputDetails": {
"truncated": false,
"result": [
{
"subjects": [
{
"math": "50",
"SST": "37 ",
"status": "ACTIVE"
}
]
}
]
},
"timeoutInSeconds": null
}')
at [Source: (String)""{\r\n "name": "Loren",\r\n "inputDetails": {\r\n "truncated": false,\r\n "result": [\r\n {\r\n "subjects": [\r\n {\r\n "math": "50",\r\n "SST": "37 ",\r\n "status": "ACTIVE"\r\n }\r\n ]\r\n }\r\n ]\r\n },\r\n "timeoutInSeconds": null\r\n}""; line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031)
at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:357)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
at Test.main(Test.java:22)
I have tried looking into similar questions available on Stakoverflow but unable to find json with similar structure which is having both string and objects.
Upvotes: 1
Views: 2613
Reputation: 5359
This is what causing your problem
request = objectMapper.writeValueAsString(request);
It affects your String json, just pass the String object to the readValue
method, it accepts also strings
Upvotes: 1
Reputation: 1893
The above code worked for me for parsing. But since you need result variable, it is inside inputDetails
. So you need to change last line as below:
List servicesResponse = (List) ((Map<String, Object>)inputParam.get("inputDetails")).get("result");
Upvotes: 1