简体   繁体   中英

Jackson ObjectMapper not able to parse string as hashmap

I want to parse a Json String to Map<String, Object> and use the same in the existing application.

import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;

public class ObjectMapperDemo {

    public static String applicantJson = "{\\\"lead_id\\\":null,\\\"finder_number\\\":null,\\\"applicant_index\\\":0,\\\"applicant_id\\\":100314,\\\"loan_application_id\\\":3000312,\\\"person\\\":{\\\"lastName[PROTECTED],\\\"firstName\\\":\\\"Cathy\\\",\\\"maternalName[PROTECTED],\\\"middleName\\\":\\\"Kimberly\\\",\\\"dateOfBirth[PROTECTED],\\\"ssn[PROTECTED],\\\"itin[PROTECTED],\\\"last4Ssn[PROTECTED],\\\"last4Itin[PROTECTED],\\\"oktaId\\\":null,\\\"suffix\\\":\\\"Sr\\\",\\\"title\\\":null,\\\"clientId\\\":null,\\\"sipClientId\\\":null,\\\"customerId\\\":null,\\\"inUSSince\\\":null},\\\"email\\\":[{\\\"home\\\":\\\"SwNjh67784@oportun.com\\\"}],\\\"address[PROTECTED],\\\"street[PROTECTED],\\\"street[PROTECTED],\\\"city[PROTECTED],\\\"state\\\":\\\"FL\\\",\\\"postalCode\\\":\\\"33144\\\"}],\\\"identification[PROTECTED],\\\"number[PROTECTED],\\\"expirationDate\\\":\\\"2023-02-02T00:00:00.000Z\\\",\\\"dateOfBirth[PROTECTED],\\\"firstName\\\":\\\"Cathy\\\",\\\"middleName\\\":\\\"Kimberly\\\",\\\"lastName[PROTECTED],\\\"maternalName[PROTECTED],\\\"suffix\\\":\\\"Sr\\\",\\\"issuingCountry\\\":\\\"US\\\",\\\"issuingState\\\":\\\"CA\\\",\\\"issuingCity[PROTECTED],\\\"issueDate\\\":null,\\\"street[PROTECTED],\\\"street[PROTECTED],\\\"postalCode\\\":\\\"94063\\\",\\\"city[PROTECTED],\\\"state\\\":\\\"CA\\\",\\\"country\\\":null}],\\\"phones\\\":[{\\\"phoneNumber\\\":\\\"689-165-2363\\\",\\\"phoneType\\\":\\\"HM\\\"},{\\\"phoneNumber\\\":\\\"332-621-7769\\\",\\\"phoneType\\\":\\\"CL\\\"}]}";

    public static Map<String, Object> ConvertValueFromObject(Object obj) {

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        return objectMapper.convertValue(obj, Map.class);

    }

    public static void main(String[] args) {
        
        Map<String, Object> testMap = ConvertValueFromObject(applicantJson);
        System.out.println(testMap.keySet());

    }

}

The stacktrace thrown is as follows:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{\"lead_id\":null,\"finder_number\":null,\"applicant_index\":0,\"applicant_id\":100314,\"loan_application_id\":3000312,\"person\":{\"lastName[PROTECTED],\"firstName\":\"Cathy\",\"maternalName[PROTECTED],\"middleName\":\"Kimberly\",\"dateOfBirth[PROTECTED],\"ssn[PROTECTED],\"itin[PROTECTED],\"last4Ssn[PROTECTED],\"last4Itin[PROTECTED],\"oktaId\":null,\"suffix\":\"Sr\",\"title\":null,\"clientId\":null,\"sipClientId\":null,\"customerId\":null,\"inUSSince\":null},\"email\":[{\"home\":\"SwNjh67784@oportun.com\"}],\"address[PROTECTED],\"street[PROTECTED],\"street[PROTECTED],\"city[PROTECTED],\"state\":\"FL\",\"postalCode\":\"33144\"}],\"identification[PROTECTED],\"number[PROTECTED],\"expirationDate\":\"2023-02-02T00:00:00.000Z\",\"dateOfBirth[PROTECTED],\"firstName\":\"Cathy\",\"middleName\":\"Kimberly\",\"lastName[PROTECTED],\"maternalName[PROTECTED],\"suffix\":\"Sr\",\"issuingCountry\":\"US\",\"issuingState\":\"CA\",\"issuingCity[PROTECTED],\"issueDate\":null,\"street[PROTECTED],\"street[PROTECTED],\"postalCode\":\"94063\",\"city[PROTECTED],\"state\":\"CA\",\"country\":null}],\"phones\":[{\"phoneNumber\":\"689-165-2363\",\"phoneType\":\"HM\"},{\"phoneNumber\":\"332-621-7769\",\"phoneType\":\"CL\"}]}')
 at [Source: UNKNOWN; byte offset: #UNKNOWN]
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4394)
    at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:4325)
    at ObjectMapperDemo.ConvertValueFromObject(ObjectMapperDemo.java:19)
    at ObjectMapperDemo.main(ObjectMapperDemo.java:25)
Caused by: 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 ('{\"lead_id\":null,\"finder_number\":null,\"applicant_index\":0,\"applicant_id\":100314,\"loan_application_id\":3000312,\"person\":{\"lastName[PROTECTED],\"firstName\":\"Cathy\",\"maternalName[PROTECTED],\"middleName\":\"Kimberly\",\"dateOfBirth[PROTECTED],\"ssn[PROTECTED],\"itin[PROTECTED],\"last4Ssn[PROTECTED],\"last4Itin[PROTECTED],\"oktaId\":null,\"suffix\":\"Sr\",\"title\":null,\"clientId\":null,\"sipClientId\":null,\"customerId\":null,\"inUSSince\":null},\"email\":[{\"home\":\"SwNjh67784@oportun.com\"}],\"address[PROTECTED],\"street[PROTECTED],\"street[PROTECTED],\"city[PROTECTED],\"state\":\"FL\",\"postalCode\":\"33144\"}],\"identification[PROTECTED],\"number[PROTECTED],\"expirationDate\":\"2023-02-02T00:00:00.000Z\",\"dateOfBirth[PROTECTED],\"firstName\":\"Cathy\",\"middleName\":\"Kimberly\",\"lastName[PROTECTED],\"maternalName[PROTECTED],\"suffix\":\"Sr\",\"issuingCountry\":\"US\",\"issuingState\":\"CA\",\"issuingCity[PROTECTED],\"issueDate\":null,\"street[PROTECTED],\"street[PROTECTED],\"postalCode\":\"94063\",\"city[PROTECTED],\"state\":\"CA\",\"country\":null}],\"phones\":[{\"phoneNumber\":\"689-165-2363\",\"phoneType\":\"HM\"},{\"phoneNumber\":\"332-621-7769\",\"phoneType\":\"CL\"}]}')
 at [Source: UNKNOWN; byte offset: #UNKNOWN]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1728)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1353)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromString(StdDeserializer.java:311)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:444)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:32)
    at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4389)
    ... 3 more

I even tried by passing the String as an Object

Map<String, Object> testMap = ConvertValueFromObject((Object) applicantJson);

实际上,作为输入提交的 JSON 是无效的(即使删除了 Intellij Idea 在将 Json 复制粘贴到 Java 编辑器时正确转换的双斜杠之后。同时,来自 Jackson 的错误消息可能有点有意义,并且不会产生误导。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM