Take the 2-minute tour ×

I have looked at GSON to parse some of my JSON objects. However, this particular JSON object is indexed like below:


            ...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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Try deserializing as a Map. You'll need to use Gson's TypeToken class to get a reference to a parameterized type:

Type type = new TypeToken<Map<String, Person>>() {}.getType();
Map<String, Person> people = gson.fromJson(json, type);
share|improve this answer
This is perfect! Thank you! –  Dan Mar 27 '12 at 3:59
add comment

Your Answer


