I use Java Spring in my project.
I need to parse JSON into java class.
Example of the JSON:
[
{
"Id":"aaa1"
"Data1":"test11"
"Data2":"test12"
"url1": "https://...someimg11.png",
"url2": "https://...someimg12.png",
},
{
"Id":"aaa2"
"Data1":"test21"
"Data2":"test22"
"url1": "https://...someimg21.png",
"url2": "https://...someimg22.png",
},
{
"Id":"aaa3"
"data1":"test31"
"data2":"test32"
"url1": "https://...someimg31.png",
"url2": "https://...someimg32.png",
}
]
And here the Java class that json above should be parsed to:
class Info{
@JsonProperty("Id")
public String id;
@JsonProperty("Data1")
public String data1;
@JsonProperty("Data2")
public String data2;
//parse to here url1 and url2 properties from json doc
public List<String> urls;
}
As you can see I have no issues to parse properties, except the last properties in JSON url1 and url2, the properties of JSON file url1 and url2 should be parsed into URLs property of type List.
My question is, how can I parse the two properties of JSON inside the single property in the class of type list of strings?
UPDATE I can use @JsonAnySetter annotation as suggested on one of the answers, but in this case, I will need to initialize the urls property, otherwise, I get this error on parsing:
Cannot invoke "java.util.List.add(Object)" because "this.urls" is null
In case if I use @JsonAnySetter annotation how can I init urls property?