gpt4 book ai didi

java - com.fasterxml.jackson.databind.exc.MismatchedInputException : Cannot construct instance of `java.util.LinkedHashMap`

转载 作者:行者123 更新时间:2023-12-04 17:29:30 25 4
gpt4 key购买 nike

我从一个项目向另一个项目发送数据。
我在 LinkedHashMap 中有我的数据。
当我发送时,我在 Json 中转换 map :objectMapper.writeValueAsString(visitToInsurer)当我尝试将 json 值转换为映射时,我会这样做:

T getJsonMessage(byte[] body) throws IOException {
return objectMapper.readValue(body, resolveGenericType());
}
Class<T> resolveGenericType() {
Class<?>[] tClass = GenericTypeResolver.resolveTypeArguments(getClass(), SomeClass.class);
return (Class<T>) tClass[0];//here i get interface of Map
}



然后我得到了一个错误: 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 ('{"id":1,"id2":2,"bDate":"2020-04-04T16:22:45.032087","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.032087","use":"1","act":true,"in":0,"vDt":null,"vDt2":null,"dCheck":null,"vFlag":true,"nFlag":false,"vFlag2":false,"nFlag2":false,"uFlag":true,"rFlag":false,"dId":1,"it":1,"rId":1,"rCode":"1","iNum":"1","iAb":"OOO\"TEST-1 \"TEST\"","dId3":2,"dId4":2,"dName":"Test","vId5":null,"vId6":null,"pDt7":null,"pDt8":null,"vDt9":null,"vDt10":null,"wCnt":null,"dId8":null,"dId9":null,"dName10":null,"dName11":"Test","cReason":null,"vd12":null,"vId13":null,"vNum14":null,"vDate15":null,"act":null,"rLst":[{"id":1,"id2":2,"bDate":"2020-04-04T16:22:45.236460","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.236460","user":"1","vId":1,"vId2":1,"dId":2,"dId2":2,"dName":"TTT","pDt":null,"pDt2":null,"vDt":null,"vDt2":null,"cBegin":"2020-05-04T00:00:00","cEnd":"2020-05-05T00:00:00","cNum":"111","amount":0,"amount2":0,"prem":0,"f":0,"pen":0,"dId5":1,"dName6":"Test","aFlag":false,"rId":11,"rCode":"11","cNum":0,"dId7":1,"pId8":null,"pId9":null,"act":null}],"vCard":{"id":1,"id2":1,"bDate":"2020-04-04T16:22:45.032087","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.032087","user":"1","act":true,"invalid":0,"rId":1,"rCode":"1","iName":"TEST \"TEST \"TEST\"","iName2":"TEST\"TEST \"TEST\"","rNum":"1","in5":"1","k77":"1","og77":"11","rBegin":"2009-09-28T00:00:00","inDate":"2009-09-28T00:00:00","outDate":null,"phone":null,"localAddress":"TEST","email":null,"iStatus":"State","sCode":"1","iCode":"1","oCode":"1","cBegin5":"1900-01-01T00:00:00","pName":null,"pName2":null,"pReg":null,"action":null,"vList":[{"id":1,"id2":"1","cCode":"1","vId":1,"vId2":1,"bDate":"2020-04-04T16:22:45.032087","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.032087","user":"1","actual":true,"rId":1,"rCode":"1","came":"ТTEST","cPhone":null,"cP":"TEST","action":null}],"vList2":[]},"vList3":[{"id":1,"id2":1,"bDate":"2020-04-04T16:22:45.095369","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.095369","user":"1","actual":true,"vId":1,"vId":1,"tNum":"1","surname":"TEST","name":"TEST","pat":"TEST","pName2":"TEST","phone":null,"kId":1,"kId2":1,"vChecks":"TEST","nFlag":false,"vFlag":true,"nFlag2":false,"vFlag2":false,"action":null}],"vList5":[]}')
更新:
谢谢你们。问题出在我的代码中,我将消息从生产者发送到订阅者。我尝试在对象的字段中发送 json 字符串,而不是在 json 字符串的字段中发送。
public class ProducerMsg {
private String body;
private Object objectBody;
}

所以在依赖中它不起作用,因为认为它已经是 Map(并且我发送了其中是 Map 的 String)

最佳答案

Please refer below code


public class JsonToMapDemo {
public static void main(String[] args) {

String json = "{\"name\":\"Akshay\",\"age\":\"23\"}";
convertJsonToMap(json);

}

private static void convertJsonToMap(String jsonString) {
try {

Map<String, Object> personMap = new ObjectMapper().readValue(jsonString, Map.class);

/*
如果我们需要 LinkedHashMap 对象

LinkedHashMap personMap = (LinkedHashMap) new ObjectMapper().readValue(jsonString, Map.class);
*/
        System.out.println(personMap);

}
catch (JsonGenerationException e)
{
e.printStackTrace();

}
catch (JsonMappingException e)
{
e.printStackTrace();

} catch (JsonProcessingException e) {

e.printStackTrace();
}

}
code output

{name=Akshay, age=23}

关于java - com.fasterxml.jackson.databind.exc.MismatchedInputException : Cannot construct instance of `java.util.LinkedHashMap` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61031589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com