I'm trying to parse a string representation of a JSON object into a List of maps. The json object will be structured something like the following:
[
{
"first_name": "fname1",
"last_name": "lname1"
},
{
"first_name": "fname2",
"last_name": "lname2"
}
.
.
.
]
The only thing that is known is the strings values (e.g "first_name", "last_name") on runtime only, therefore I cannot create any predefined classes (like usually used in fromJson() method).
I have tried to create the following function (after I saw some examples online) and it didn't work:
public List<Map<String, Object>> fromJson(String jsonAsString) {
final JsonElement jsonElement = this.jsonParser.parse(jsonAsString);
List<Map<String, Object>> myList= new ArrayList<Map<String, Object>>();
Type listType = TypeToken.get(myList.getClass()).getType();
myList= (new Gson()).fromJson(jsonElement, listType);
return myList;
}
The result of this function is a list (length == 2 in the above case) but instead of 2 maps i'm getting 2 Objects.
Any help? Thanks!