我想解析一个Json String到Map<String,>并在现有的应用程序中使用相同的。
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());
}
}
抛出的堆栈跟踪如下:
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
我甚至尝试将String作为对象传递
Map<String, Object> testMap = ConvertValueFromObject((Object) applicantJson);
实际上,作为输入提交的JSON是无效的(即使删除了Intellij Idea在复制粘贴JSON到java编辑器时适当转换的双斜杠)。与此同时,来自Jackson的错误信息可能有点有意义,更少误导。