Java array mapping - Jackson

Given the following JSON

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

I'm tired of using @JsonProperty and @JsonCreator to display the values โ€‹โ€‹of "m1" and "m2" , but 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.jack son.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.jack son.annotation.JsonCreator()}] has no property name annotation; must have name when multiple-paramater constructor annotated as Creator 

How can "arr" fields be displayed using Jackson's annotation? Thanks.

UPDATE

I also tried to include @JsonProperty("arr") private Card[] cards; to class f3 , but that didn't help either.

UPDATE 2

I changed the type of the array in the above update, and now I am not getting an error, but it does not behave the way I want (I would like to process the display).

Is there any chance that the error can be explained in such a way that I can correct them.

+4
source share
1 answer

If m1 and m2 are not hardcoded properties, you should use TypeReference to convert json to java map. Watch the record

Otherwise, you can map "arr" to your Arr class with the strings m1 and String m2 as fields.

0
source

Source: https://habr.com/ru/post/1438940/


All Articles