gpt4 book ai didi

json - MismatchedInputException:无法构造 `java.util.LinkedHashMap`的实例

转载 作者:行者123 更新时间:2023-12-02 13:36:16 27 4
gpt4 key购买 nike

我尝试在LinkedHashMap测试用例中将JSON映射为kotlin。当我使用jsonobject转换为jackson-objectmapper时,出现以下问题,请帮我解决这个问题。

construct instance of java.util.LinkedHashMap (although at least one 
Creator exists): no String-argument constructor/factory method to
deserialize from String value ('{e123f1dd-6c54-4b01-90e6-
d701748f0851=class MyClass {

我的 Kotlin测试用例
fun `testcase success`() {

val mapId = UUID.fromString("e123f1dd-6c54-4b01-90e6-d701748f0851");
val Idno = UUID.randomUUID();

val order = MyClass();

val map : LinkedHashMap<String, MyClass> = LinkedHashMap();

order.collectiveSignature=true
order.docId="5436576"
order.status=OnboardOrderPoaStatusTPL.NOT_SIGNED
order.createdAt=createdDate
order.updatedAt=updatedDate
order.portingAt=OffsetDateTime.parse("2018-09-21T14:22:24.682+02:00");
order.fastPorting=true
order.owner=MyAnotherClass();
order.owner.company="test"
order.owner.firstName="test"
order.owner.lastName="test"
order.owner.city="test"
order.owner.zip="5436"
order.owner.street="test"
order.owner.house="test"


map["e123f1dd-6c54-4b01-90e6-d701748f0851"] = order;

val body = """{

"mapId": "$mapId",
"Idno": "$Idno",
"name": "Abishek",
"poas":"$map"

}""".trimIndent()

val mapper = ObjectMapper()
mapper.findAndRegisterModules()

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, false);


val input = mapper.readValue<MainClass>(body)


}

最佳答案

问题是poas值包含Map的字符串表示形式,其中包含MyClass的字符串表示形式。
确实,创建称为JsonMyClass.toString()表示形式的目的和toString的目的是显示调试信息。

如何解决?

使用ObjectMapper,您应该将Map转换为json,而不是使用toString()

  val body = """{

"mapId": "$mapId",
"Idno": "$Idno",
"name": "Abishek",
"poas": "${mapper.writeValue(map)}"

}""".trimIndent()

关于json - MismatchedInputException:无法构造 `java.util.LinkedHashMap`的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56372481/

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