4

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.

Mr.
  • 6,516
  • 11
  • 51
  • 66
  • try @JsonProperty("arr") private List cards – Nailgun Oct 10 '12 at 15:14
  • @Nailgun, I get the error `No suitable constructor found for type [simple type, class MyObj$Obj$Card]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.StringReader@751bbe4f; line: 1, column: 201] (through reference chain: MyObj["obj"]->Obj["cards"])`. Any clue? – Mr. Oct 10 '12 at 15:19
  • Could you show the classes you use? – eugen Oct 10 '12 at 17:11
  • @eugen, any chance you could elaborate on the error and what causes it? – Mr. Oct 11 '12 at 08:03
  • Without the classes I am not sure, but if they are inner classes (non static) it is normal that it fails, jackson can not deserialize to this kind of types. – eugen Oct 11 '12 at 09:50

1 Answers1

0

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.

Community
  • 1
  • 1
Nailgun
  • 3,499
  • 3
  • 27
  • 44
  • But 'arr' is an array. Hence, it should be mapped to an array or a list of object of which the array holds. Any chance for a snippet regarding your last suggestion? – Mr. Oct 10 '12 at 15:30
  • I'm not sure I understand what you want. You can always use Map.entrySet() to access to these objects as a collection. I can imagine how to deserialize {"messages":["msg 1","msg 2","msg 3"]} or {"messages":["name":"val1","name":"val2"]} to a list but not your example. – Nailgun Oct 10 '12 at 15:40
  • could you please elaborate on the error, i.e. what does the error mean. maybe this will let me fix the issue. – Mr. Oct 11 '12 at 08:00