locked
cannot implicitly convert type system.collections.Generic.Ienumerable RRS feed

  • Question

  • User-1355965324 posted

    I am trying to parse json string calling from API call . The json string has group part. I am getting the Json data as given below

    public class Weather
        {
            
            public string  id { get; set; }
            public string weather_state_name { get; set; }
            public string weather_state_abbr { get; set; }
            public string wind_direction_compass { get; set; }
            public string created { get; set; }
            public string applicable_date { get; set; }
            public string min_temp { get; set; }
            public string max_temp { get; set; }
            public string the_temp { get; set; }
            public string wind_speed { get; set; }
            public string wind_direction { get; set; }
            public string air_pressure { get; set; }
            public string Humidity { get; set; }
            public string  Visibllity { get; set; }
            public string Predictability { get; set; }   
           
    
        }
    
    
    {
        "consolidated_weather": [
            {
                "id": 4577625064341504,
                "weather_state_name": "Heavy Rain",
                "weather_state_abbr": "hr",
                "wind_direction_compass": "WSW",
                "created": "2020-07-14T19:35:14.577740Z",
                "applicable_date": "2020-07-14",
                "min_temp": 11.11,
                "max_temp": 15.05,
                "the_temp": 14.32,
                "wind_speed": 6.570953330777592,
                "wind_direction": 254.13274105640758,
                "air_pressure": 1016.5,
                "humidity": 85,
                "visibility": 7.654361031575599,
                "predictability": 77
            },
            {
                "id": 4896540210495488,
                "weather_state_name": "Showers",
                "weather_state_abbr": "s",
                "wind_direction_compass": "WNW",
                "created": "2020-07-14T19:35:17.569534Z",
                "applicable_date": "2020-07-15",
                "min_temp": 12.31,
                "max_temp": 17.03,
                "the_temp": 16.509999999999998,
                "wind_speed": 7.600821124862802,
                "wind_direction": 284.49357944800784,
                "air_pressure": 1015.5,
                "humidity": 82,
                "visibility": 13.558008729022509,
                "predictability": 73
            },
    ]
    "title": "Texas",
        "location_type": "City", 
        "timezone": "US"

    My Models

    public class WeatherList
        {
            public IEnumerable<Weather> consolidated_weather { get; set; }
        }
    }
    
    public class Weather
        {
            
            public string  id { get; set; }
            public string weather_state_name { get; set; }
            public string weather_state_abbr { get; set; }
            public string wind_direction_compass { get; set; }
            public string created { get; set; }
            public string applicable_date { get; set; }
            public string min_temp { get; set; }
            public string max_temp { get; set; }
            public string the_temp { get; set; }
            public string wind_speed { get; set; }
            public string wind_direction { get; set; }
            public string air_pressure { get; set; }
            public string Humidity { get; set; }
            public string  Visibllity { get; set; }
            public string Predictability { get; set; }   
           
    
        }
    

    Then I am trying to add the necessary data needed to show in  IEnumerable<Weather> to list but the error is coming cannot implicitly convert type system.collections.Generic.Ienumerable 

     public async Task<IEnumerable<Weather>> GetWeatherAsync(string woied)
            {
    
                var url = SD.APIBaseUrl + woied;
                var request = new HttpRequestMessage(HttpMethod.Get, url);
                var client = _clientFactory.CreateClient();
                HttpResponseMessage response = await client.SendAsync(request);
               
                IEnumerable<Weather> weather = new List<Weather>();
                WeatherList weatherList = new WeatherList();
                
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var jsonString = await response.Content.ReadAsStringAsync();
                   
                    weatherList = JsonConvert.DeserializeObject<WeatherList>(jsonString);
                    return weatherList;
                }
                return null;
            }

    I am trying to store the following data only from the API jsonstring please help

    "id": 4577625064341504,
                "weather_state_name": "Heavy Rain",
                "weather_state_abbr": "hr",
                "wind_direction_compass": "WSW",
                "created": "2020-07-14T19:35:14.577740Z",
                "applicable_date": "2020-07-14",
                "min_temp": 11.11,
                "max_temp": 15.05,
                "the_temp": 14.32,
                "wind_speed": 6.570953330777592,
                "wind_direction": 254.13274105640758,
                "air_pressure": 1016.5,
                "humidity": 85,
                "visibility": 7.654361031575599,
                "predictability": 77

    Wednesday, July 15, 2020 6:12 AM

All replies

  • User-406479672 posted

    There is nothing wrong with your code, but the end of json is strange.
    I think if you change last bit to and closing the json with "}" then it will work

    "title": "Texas",
        "location_type": "City", 
        "timezone": "US"
    Wednesday, July 15, 2020 7:13 AM
  • User-474980206 posted

    IEnumable<Weather> is an interface, and JsonConvert can not create one. you need to supply a custom constructor (see newtonsoft docs) or change model to use concrete class:

    public class WeatherList
        {
            public List<Weather> consolidated_weather { get; set; }
        }
    }
    

    Wednesday, July 15, 2020 3:23 PM
  • User2078676645 posted

    Hi,

     public async Task<IEnumerable<Weather>> GetWeatherAsync(string woied)
            {
    
                var url = SD.APIBaseUrl + woied;
                var request = new HttpRequestMessage(HttpMethod.Get, url);
                var client = _clientFactory.CreateClient();
                HttpResponseMessage response = await client.SendAsync(request);
               
                IEnumerable<Weather> weather = new List<Weather>();
                WeatherList weatherList = new WeatherList();
                
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var jsonString = await response.Content.ReadAsStringAsync();
                   
                    weatherList = JsonConvert.DeserializeObject<WeatherList>(jsonString);
                    return weatherList;
                }
                return null;
            }

    From the obtained json format, the serialization has become a dictionary, and the deserialization will get the object WeatherList, but the return type of this function is IEnumerable<Weather>.

    Regards,

    Evern

    Thursday, July 16, 2020 5:03 AM