Почему так работает for?

Получаю данные ретрофитом с апи.

Собственно получаю вот такой json:

{
  "Global": {
    "item1": 1
    "item2": 2
  },
  "Countries": [
    {
      "Country": "ALA Aland Islands",
      ...other attributes
    },
    {
      "Country": "Afghanistan",
      ...other attributes


@GET("summary")
Call<SummaryResponse<TotalStatsModel, List<CountryModel>>> getData();

Retrofit в методе onResponse возвращает мне список моделей CountryModel, но при попытке перебора через foreach

for (CountryModel country : countries) {

получаю ошибку

Incompatible types. Required: Object Found: CountryModel.

Если написать

for (Object country : countries) {

То у IDE не видит методов модели, хотя county instanceof CountryModel == true

Почему это так работает? Как мне правильно перебрать список моделей CountryModel?


Ответы (1 шт):

Автор решения: Alexander Chernin

Попробуйте так:

for (Object country : countries) {
    if( country instanceof CountryModel ) {
        CountryModel countryModel = (CountryModel)country;
        ...
    }
}
→ Ссылка