I have a json file
{"apple" : [
{
"first" : 3,
"second" : 5,
}
],
"orange" : [
{
"fst" : 7,
"snd" : 8,
}
],
}
a helper class for deserialization
public class Helper {
private int num1;
private int num2;
public Helper(Helper other) {
this.num1 = other.num1;
this.num2 = other.num2;
}
public int getNum1() {
return this.num1;
}
public int getNum2() {
return this.num2;
}
}
A java class for deserialization, which I need to change the JSON keys to be compatible with instance names of the Helper
class.
public class MyDesClass {
@SerializedName(value = "apple.first", alternate = "apple.num1")
@SerializedName(value = "apple.seconds", alternate = "apple.num2")
private final Helper[] apple;
public MyDesClass(Helper[] apple) {
this.apple = apple;
}
}
And, also inside main.java I have:
/* .... */
Reader f = new FileReader( PATH_TO_THE_JSON_FILE);
Gson gson = new Gson();
GameBoard gameBoard = gson.fromJson(f, MyDesClass.class);
/* .... */
My question is how can I convert two values (e.g. first
and second
) at the same time (e.g. to num1
and num2
here) in MyDesClass
? Currently, I get SerializedName is not a repeatable annotation type
error.