示例#1
0
        public String WeatherDetail(string City)
        {
            string appId = "10802a322cab84354d59fb3e37ee800e";

            string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units=metric&cnt=1&APPID={1}", City, appId);

            using (WebClient client = new WebClient())
            {
                string json = client.DownloadString(url);

                Models.OpenWeatherMapModels.RootObject weatherInfo = (new JavaScriptSerializer()).Deserialize <Models.OpenWeatherMapModels.RootObject>(json);

                Models.OpenWeatherMapModels.ResultViewModel rslt = new Models.OpenWeatherMapModels.ResultViewModel();

                rslt.Country       = weatherInfo.sys.country;
                rslt.City          = weatherInfo.name;
                rslt.Lat           = Convert.ToString(weatherInfo.coord.lat);
                rslt.Lon           = Convert.ToString(weatherInfo.coord.lon);
                rslt.Description   = weatherInfo.weather[0].description;
                rslt.Humidity      = Convert.ToString(weatherInfo.main.humidity);
                rslt.Temp          = Convert.ToString(weatherInfo.main.temp);
                rslt.TempFeelsLike = Convert.ToString(weatherInfo.main.feels_like);
                rslt.TempMax       = Convert.ToString(weatherInfo.main.temp_max);
                rslt.TempMin       = Convert.ToString(weatherInfo.main.temp_min);
                rslt.WeatherIcon   = weatherInfo.weather[0].icon;

                var jsonstring = new JavaScriptSerializer().Serialize(rslt);

                return(jsonstring);
            }
        }
示例#2
0
        public IActionResult Weather()
        {
            Models.OpenWeatherMapModels.ResultViewModel model = new Models.OpenWeatherMapModels.ResultViewModel();

            return(View("weather", model));
        }