zoukankan      html  css  js  c++  java
  • 查看天气

    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WeatherReport
    {
    class Program
    {
    static void Main(string[] args)
    {
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("https://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20woeid%20in%20(2151330%20)&format=json");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    
    GetWeather(client).Wait(); 
    }
    
    static async Task GetWeather(HttpClient cons)
    {
    using (cons)
    {
    HttpResponseMessage res = await cons.GetAsync("");
    
    res.EnsureSuccessStatusCode();
    if (res.IsSuccessStatusCode)
    {
    string weather = await res.Content.ReadAsStringAsync();
    
    JObject jobj = JObject.Parse(weather);
    JToken jToken = jobj.First;
    string WeatherState = jToken.First["results"]["channel"]["item"]["condition"].ToString();
    WeatherInfo report = Newtonsoft.Json.JsonConvert.DeserializeObject<WeatherInfo>(WeatherState);
    Console.WriteLine("
    ");
    
    Console.WriteLine("Weather Station: Beijing");
    Console.WriteLine("Temperature Details");
    Console.WriteLine("-----------------------------------------------------------");
    Console.WriteLine("温度(C): " + (report.temp - 32) * 0.55);// Converted from Fahrenheit to Celsius 
    Console.WriteLine("天气状态: " + report.text);
    Console.WriteLine("时间: " + report.date);
    Console.ReadLine();
    }
    }
    }
    }
    
    public class WeatherInfo
    {
    public string date { get; set; }
    
    public long temp { get; set; }
    
    public string text { get; set; }
    
    
    }
    }
  • 相关阅读:
    基础
    基础
    基础
    基础
    基础
    基础
    基础
    基础
    Gym102361A Angle Beats(直角三角形 计算几何)题解
    Petrozavodsk Summer Training Camp 2016H(多标记线段树)题解
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/8884087.html
Copyright © 2011-2022 走看看