public string GetCurrentWeatherJson(string url) { using (WebClient client = new WebClient()) { json = client.DownloadString(url); } WeatherCurrent WeatherCurrent = (new JavaScriptSerializer()).Deserialize <WeatherCurrent>(json); JavaScriptSerializer js = new JavaScriptSerializer(); json = js.Serialize(WeatherCurrent); return(json); }
public static WeatherCurrent GetWeatherCurrent(JObject response) { var error = GetServerErrorFromResponse(response); if (!String.IsNullOrEmpty(error)) { return(null); } var weatherCurrent = new WeatherCurrent(); if (response["sys"] != null) { weatherCurrent.Country = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["sys"]["country"]))); } if (response["weather"] != null) { weatherCurrent.Title = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["weather"][0]["main"]))); weatherCurrent.Description = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["weather"][0]["description"]))); weatherCurrent.Icon = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["weather"][0]["icon"]))); weatherCurrent.ConditionID = Convert.ToInt32(response["weather"][0]["id"]); } if (response["main"] != null) { weatherCurrent.Temp = Convert.ToDouble(response["main"]["temp"]); weatherCurrent.TempMax = Convert.ToDouble(response["main"]["temp_max"]); weatherCurrent.TempMin = Convert.ToDouble(response["main"]["temp_min"]); weatherCurrent.Humidity = Convert.ToDouble(response["main"]["humidity"]); } if (response["wind"] != null) { weatherCurrent.WindSpeed = Convert.ToDouble(response["wind"]["speed"]); } weatherCurrent.Date = DateTime.UtcNow; weatherCurrent.City = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["name"]))); weatherCurrent.CityId = Convert.ToInt32(response["id"]); return(weatherCurrent); }
public static SingleResult<WeatherCurrent> GetWeatherCurrent(JObject response) { var error = GetServerErrorFromResponse(response); if (!String.IsNullOrEmpty(error)) return new SingleResult<WeatherCurrent>(null, false, error); var weatherCurrent = new WeatherCurrent(); if (response["sys"] != null) { weatherCurrent.Country = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["sys"]["country"]))); } if (response["weather"] != null) { weatherCurrent.Title = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["weather"][0]["main"]))); weatherCurrent.Description = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["weather"][0]["description"]))); weatherCurrent.Icon = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["weather"][0]["icon"]))); } if (response["main"] != null) { weatherCurrent.Temp = Convert.ToDouble(response["main"]["temp"]); weatherCurrent.TempMax = Convert.ToDouble(response["main"]["temp_max"]); weatherCurrent.TempMin = Convert.ToDouble(response["main"]["temp_min"]); weatherCurrent.Humidity = Convert.ToDouble(response["main"]["humidity"]); } if (response["wind"] != null) { weatherCurrent.WindSpeed = Convert.ToDouble(response["wind"]["speed"]); } weatherCurrent.Date = DateTime.UtcNow; weatherCurrent.City = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Convert.ToString(response["name"]))); weatherCurrent.CityId = Convert.ToInt32(response["id"]); return new SingleResult<WeatherCurrent>(weatherCurrent, true, TimeHelper.MessageSuccess); }
public void Run() { var currentItem = GetDatasourceItem(); var weatherInfo = new WeatherCurrent(); var apiSource = (LookupField)currentItem.Fields["ApiSource"]; httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); using (httpClient) { HttpResponseMessage response = httpClient.GetAsync(apiSource.TargetItem.Uri.Path).Result; if (response.IsSuccessStatusCode) { var data = response.Content.ReadAsStringAsync().Result; weatherInfo = Newtonsoft.Json.JsonConvert.DeserializeObject <WeatherCurrent>(data); } ; } }
public ActionResult CityWeatherList() { var currentItem = GetDatasourceItem(); var weatherInfo = new WeatherCurrent(); if (currentItem == null) { return(View("WeatherWidget", weatherInfo)); } var locationApi = @"http://ip-api.com/json/";//currentItem.Fields["LocationApi"].Value; var location = GetGeoLocation(locationApi); var targetItem = ((LookupField)currentItem.Fields["ApiSource"]).TargetItem; var appId = targetItem.Fields["AppId"].Value; var baseUrl = targetItem.Fields["AppWeatherBaseUrl"].Value; var destinationUrl = targetItem.Fields["SearchByDestinationRelativeUrl"].Value; var temparatureUnit = targetItem.Fields["TemperatureUnit"].Value; var request = new WeatherRequestModel() { AppId = appId, BaseURL = baseUrl, location = location, RelativeURL = destinationUrl, Unit = temparatureUnit }; IWeatherRepository weatherServiceRepository = new WeatherRepository(); var weatherInfoString = weatherServiceRepository.GetWeatherByCity(request); weatherInfo = Newtonsoft.Json.JsonConvert.DeserializeObject <WeatherCurrent>(weatherInfoString); return(View("WeatherWidget", weatherInfo)); }