我正在用Jackson解析以下外部XML。
<SomeRootObject>
<Events>
<Event>
<EventID>248739296</EventID>
...
<Event>1709</Event>
...
Run Code Online (Sandbox Code Playgroud)
我为“事件”定义了一个POJO。
@JacksonXmlRootElement(localName = "Event")
public class MyEvent {
@JsonProperty("EventID")
public String eventID;
...
@JsonProperty("Event")
public int event;
...
Run Code Online (Sandbox Code Playgroud)
如您所见,此POJO中的一个字段也被映射为“事件”。因此,杰克逊抱怨说它无法从事件中创建一个int:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token
at [Source: java.io.StringReader@12417468; line: 1, column: 280] (through reference chain: be.parkd.api.tnt.ram.model.RamEvents[“Event”]->java.util.ArrayList[0]->be.parkd.api.tnt.ram.model.RamEvent[“Event”]).
Run Code Online (Sandbox Code Playgroud)
杰克逊可以处理此案吗?
我想到的一个肮脏的解决方法是对XML进行预处理以更改基础事件,但是我希望使用一种更干净的解决方案。
让我们说我有两种类型的别名:
type alias A = {...}
type alias B = {...}
Run Code Online (Sandbox Code Playgroud)
和联合类型
type Content = A | B
Run Code Online (Sandbox Code Playgroud)
和模型类型
type alias Model= {cont : Content, ...}
init : Content -> Model
init cont = {cont = cont, ...}
Run Code Online (Sandbox Code Playgroud)
如何将类型A的记录传递给init.
a : A
a = {...}
init a
Run Code Online (Sandbox Code Playgroud)
抛出以下错误:
Detected errors in 1 module.
## ERRORS in Main.elm ##########################################################
-- TYPE MISMATCH ------------------------------------------------------ Main.elm
The 1st argument to function `init` has an unexpected type.
78| init e
^
As I infer …Run Code Online (Sandbox Code Playgroud)