예제 #1
0
        private void getMetaWeatherData()
        {
            System.Net.Http.HttpClient client2 = new System.Net.Http.HttpClient();
            var         response2     = client2.GetAsync("https://www.metaweather.com/api/location/" + id).Result;
            var         streamReader2 = new StreamReader(response2.Content.ReadAsStreamAsync().Result);
            var         data          = streamReader2.ReadToEnd();
            WeatherInfo weatherInfo   = JsonSerializer.Deserialize <WeatherInfo>(data);

            Console.WriteLine(location + " max temp: " + weatherInfo.consolidated_weather[0].max_temp + " min temp: " + weatherInfo.consolidated_weather[0].min_temp);

            writeWeatherDataToFile(weatherInfo);
        }
예제 #2
0
 private void writeWeatherDataToFile(WeatherInfo weatherInfo)
 {
     try
     {
         string       fileName = "weather" + location + DateTimeOffset.Now.ToUnixTimeSeconds() + ".json";
         FileStream   stream   = new FileStream(fileName, FileMode.CreateNew);
         StreamWriter sw       = new StreamWriter(stream);
         sw.Write(JsonSerializer.Serialize(weatherInfo));
         sw.Close();
         Console.WriteLine("Writing to {0} successful", fileName);
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception: " + e.Message);
     }
 }