I have the following class:
class Person
{
String Name;
}
I read in the following Json data:
{
"People": [{
"Name": "Test"
},
{
"Name": "Hello"
}
]
}
I do this using a generic class as these could be students, professors etc..
public BusinessData<T> GetDataFromJson(String Url)
{
// Get the data from the URL
return new Gson().fromJson(data, new TypeToken<BusinessData<T>>(){}.getType());
}
public List<People> GetPeople()
{
return this.GetDataFromJson(Url).People;
}
Therefore.. I should now have a List
of the class People
.. However, whenever I try and do the following:
for(People person : PeopleList)
{
System.out.println(person.getName());
}
I get the following error:
com.google.gson.internal.LinkedTreeMap cannot be cast to com.proj.business.Models.Person
Which to me makes no sense, because:
I pass in
List<People>
to the method I'm trying to use the data with just fineIf I just do a standard
System.out.println(people.get(1))
then the data is printed out just fine in the classPerson
Can anyone please suggest what I am doing wrong here?