gpt4 book ai didi

java - XML 反序列化导致 JsonMappingException : Can not deserialize instance of class out of VALUE_STRING token

转载 作者:行者123 更新时间:2023-12-01 08:47:17 25 4
gpt4 key购买 nike

我有这样的xml:

<game quarter="2">
<quarter number="1">
...
</quarter>
<quarter number="2">
...
</quarter>
</game>

还有我的 POJO 类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Game {

@JacksonXmlElementWrapper(localName = "quarter")
public Quarter[] quarters;

当我尝试使用以下方法反序列化 xml 时:

xmlMapper.readValue(getFileContent(PBP_XML_PATH), Game.class);

我得到:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of Game$Quarter[] out of VALUE_STRING token
at [Source: java.io.StringReader@5f45632; line: 4, column: 1] (through reference chain: Game["quarter"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:275)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:179)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:20)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:499)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:108)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)

看起来 Jackson 无法处理同名的属性和属性。

最佳答案

正如它所显示的,这是 jackson-dataformat-xml-2.9.0 版本中的一个已知问题:

Attribute / element name collision cannot be configured during deserialization

If an element has an attribute and sub-element with the same name, the attribute seems to be ignored and the element value used

Currently there is no way to handle synonyms (either between attribute or element; or between properties with different namespace).

关于java - XML 反序列化导致 JsonMappingException : Can not deserialize instance of class out of VALUE_STRING token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42598528/

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