I have looked at GSON to parse some of my JSON objects. However, this particular JSON object is indexed like below:
{
{
"1":
{"name":"Mike","age":"27"},
"2":
{"name":"Sarah","age":"23"},
"3":
{"name":"Jenny","age":"19"},
"4":
{"name":"Joe","age":"24"},
"5":
{"name":"Bob","age":"21"},
...and so on
}
}
From my understanding, each key corresponds to a variable name for the appropriate GSON object. For example, for “5”, I would have a class like this:
public class Person {
private String name;
private int age;
}
However, in this case, how will I take care of “1”, “2”, “3”, “4”, and “5”? There can be any number of indices so I cannot simply name the variables “1”, “2”, “3”, “4”, and “5” (and it goes against variable naming rules). Can GSON help me in this case?
Try deserializing as a Map. You’ll need to use Gson’s TypeToken class to get a reference to a parameterized type: