简体   繁体   中英

Java - Jackson array mapping

Given the following JSON

{
    "obj" : {
        "f1" : "blah",
        "f2" : "blah",
        "f3" : [{"z1" : "blah", "arr" : [{"m1" : "blah", "m2" : "blah"}]}]
    }
}

I have tired using @JsonProperty and @JsonCreator in order to map "m1" and "m2" values, but with no luck.

With @JsonCreator public Card(Map<String,Object> props) { } i get the following error:

Argument #0 of constructor [constructor for MyObj$Obj$Card, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator()}] has no property name annotation; must have name when multiple-paramater constructor annotated as Creator

With @JsonCreator public Card(@JsonProperty("m1") String m1, @JsonProperty("m2") String m2,){} I get the following error:

Argument #0 of constructor [constructor for MyObj$Obj$Card, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator()}] has no property name annotation; must have name when multiple-paramater constructor annotated as Creator

How can the fields of "arr" be mapped using Jackson annotation? Thanks.

UPDATE

I also tried to include @JsonProperty("arr") private Card[] cards; on f3 class, but it did not help either.

UPDATE 2

I have changed the array type in the update above, and now I do not get the error but it does not behaves as I wish (I would like to handle the mapping).

Is there any chance one could explain the error in a way that I could correct them.

If m1 and m2 are not hardcoded properties than you should use TypeReference to convert json to java map. See this entry

Else you can map "arr" with your Arr class having String m1 and String m2 as fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM