Exemplo n.º 1
0
        private static Weather ParseWeather(string zipCode, string json)
        {
            JsonValue jsonObject = JsonValue.Parse(json);
            JsonValue location = jsonObject["value"]["items"][0]["loc"];
            JsonValue currentConditions = jsonObject["value"]["items"][0]["cc"];
            JsonValue forecastConditions = jsonObject["value"]["items"][0]["dayf"]["day"];

            WeatherInformation[] forecastItems = new WeatherInformation[4];
            Weather weather = new Weather(zipCode, forecastItems);

            weather.Location = location["dnam"];
            weather.TimeStamp = currentConditions["lsup"];

            weather.Description = currentConditions["t"];
            weather.Temperature = currentConditions["tmp"];
            weather.ImageUri = "/Images/" + currentConditions["icon"] + ".png";

            for (int i = 1; i < 5; i++) {
                WeatherInformation wi = new WeatherInformation();
                JsonValue forecast = forecastConditions[i];
                JsonValue dayInfo = forecast["part"][0];
                JsonValue nightInfo = forecast["part"][1];

                wi.Day = forecast["t"].ToString().Substring(1, 3);
                wi.Date = forecast["dt"];
                wi.Low = forecast["low"];
                wi.High = forecast["hi"];
                wi.DayDescription = dayInfo["t"];
                wi.DayImageUrl = "/Images/" + dayInfo["icon"] + ".png";
                wi.NightDescription = nightInfo["t"];
                wi.NightImageUrl = "/Images/" + nightInfo["icon"] + ".png";

                forecastItems[i - 1] = wi;
            }

            return weather;
        }
Exemplo n.º 2
0
        private void LookupWeatherCallback(IAsyncResult asyncResult)
        {
            if (asyncResult == _currentAsyncResult) {
                _currentAsyncResult = null;

                WeatherService weatherService = (WeatherService)asyncResult.AsyncState;

                _weather = weatherService.EndGetWeather(asyncResult);
                if (_weather != null) {
                    XApplication.Current.Settings["ZipCode"] = _zipCode;
                }

                RaisePropertyChanged("Weather", "IsLoading", "Status", "ZipCode");
            }
        }