public void getWeather(string cityName) { try { using (WebClient wc = new WebClient()) { var json = ""; json = wc.DownloadString(string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&units=metric&cnt=6", cityName, APIKey)); var deserializedUrl = JsonConvert.DeserializeObject <WeatherJSON>(json); WeatherJSON result = deserializedUrl; city = result.name; cTemp = result.main.temp; fTemp = Math.Round((result.main.temp * 9 / 5) + 32); date = DateTime.Now.ToString("yyyy/MM/dd"); time = string.Format(DateTime.Now.ToString("HH:mm:ss")); icon = result.weather[0].icon; condition = result.weather[0].description; feelsLikeC = result.main.feels_like; feelsLikeF = Math.Round((result.main.feels_like * 9 / 5) + 32); windSpeed = result.wind.speed; dateTime.Text = time; isCityExisting = true; setLabels(); } } catch (WebException) { MessageBox.Show("Enter a correct city"); isCityExisting = false; } }
private async void InitJSON() { var url = "http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/353412?" + "apikey=CxILqfbYMdKI30fs02iXyl2JZJdF2MeU&language=vi-vn&metric=true "; var list = await WeatherJSON.GetJSON(url) as List <WeatherJSON>; Debug.WriteLine("Count: " + list.Count); list.ForEach(it => { var match = Regex.Matches(it.DateTime, "T(?<time>\\d+):")[0].Groups["time"].Value; if (int.Parse(match) > 12) { match = (int.Parse(match) - 12) + "PM"; } else { match += "PM"; } it.DateTime = match; it.Temperature.Value += "\u00B0"; it.WeatherIcon = string.Format("http://vortex.accuweather.com/adc2010/images/slate/icons/{0}.svg", it.WeatherIcon); WeatherEachHours.Add(it); }); WeatherDescriptionTextBlock.Text = list[0].IconPhrase; PrecipitationType.Text = list[0].IconPhrase; WeatherDescriptionTextBlock.Text = list[0].Temperature.Value; }