拨打5天/ 3小时预报数据

时间:2017-04-27 18:42:08

标签: c# openweathermap

我正在开展一个项目,我需要从openweathermap获取5天/ 3小时的预测数据,我已设法让它工作,我现在可以获得所选位置的当前预测,但我可以'弄清楚如何获得5天/ 3小时的预测数据! 有什么帮助吗?

mian部分代码:

string request = "http://api.openweathermap.org/data/2.5/weather?q=Washington&APPID=Key";

var jsonres = wcumum.DownloadString(request);
var objectres = JsonConvert.DeserializeObject<OpenWeatherMap.Root>(jsonres);
OpenWeatherMap.Root resultobj = objectres;
string ctry = string.Format("Country\t\t: {0}\n", resultobj.sys.country);
string cty = string.Format("City\t\t: {0}\n", resultobj.name);
string Wind = string.Format("Wind\t\t: {0}\n", resultobj.wind.speed);

public class Coord
{
    public double lon { get; set; }
    public double lat { get; set; }
}

public class Sys
{
    public int type { get; set; }
    public int id { get; set; }
    public double message { get; set; }
    public string country { get; set; }
    public int sunrise { get; set; }
    public int sunset { get; set; }

}

public class Weather
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

public class Main
{
    public double temp { get; set; }
    public int humidity { get; set; }
    public double pressure { get; set; }
    public double temp_min { get; set; }
    public double temp_max { get; set; }
}

public class Wind
{
    public double speed { get; set; }
    public double gust { get; set; }
    public double deg { get; set; }
}

public class Rain
{
    public string rain { get; set; }

}

public class Clouds
{
    public int all { get; set; }
}

public class Root
{
    public Coord coord { get; set; }
    public Sys sys { get; set; }
    public List<Weather> weather { get; set; }
    public string @base { get; set; }
    public Main main { get; set; }
    public Wind wind { get; set; }
    public Dictionary<string, double> rain { get; set; }
    public Clouds clouds { get; set; }
    public int dt { get; set; }
    public int id { get; set; }
    public string name { get; set; }
    public int cod { get; set; }
}

1 个答案:

答案 0 :(得分:0)

使用示例:

void Main()
{
    var testData = "http://samples.openweathermap.org/data/2.5/forecast?q=Washington&appid=...";
    var json = new WebClient().DownloadString(testData);
    var result = JsonConvert.DeserializeObject<RootObject>(json);

    Console.WriteLine("City:" + result.city.name);
    Console.WriteLine();

    const double AbsoluteZero = -273.15;
    foreach (var forecast in result.list)
    {
        Console.WriteLine("{0:ddd hh:MM}: {1:f2}c ~ {2:f2}c ~ {3:f2}c",
            forecast.dt,
            AbsoluteZero + forecast.main.temp, 
            AbsoluteZero + forecast.main.temp_min, 
            AbsoluteZero + forecast.main.temp_max); 
    }
}

// Define other methods and classes here
public class MainDetail
{
    public double temp { get; set; }
    public double temp_min { get; set; }
    public double temp_max { get; set; }
    public double pressure { get; set; }
    public double sea_level { get; set; }
    public double grnd_level { get; set; }
    public int humidity { get; set; }
    public double temp_kf { get; set; }
}

public class Weather
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

public class Clouds
{
    public int all { get; set; }
}

public class Wind
{
    public double speed { get; set; }
    public double deg { get; set; }
}

public class Rain
{
    public double rain { get; set; }
}

public class Sys
{
    public string pod { get; set; }
}

public class Forecast
{
    [JsonConverter(typeof(UnixTimestampConverter))]
    public DateTime dt { get; set; }
    public MainDetail main { get; set; }
    public List<Weather> weather { get; set; }
    public Clouds clouds { get; set; }
    public Wind wind { get; set; }
    public Rain rain { get; set; }
    public Sys sys { get; set; }
    public string dt_txt { get; set; }
}

public class Coord
{
    public double lat { get; set; }
    public double lon { get; set; }
}

public class City
{
    public int id { get; set; }
    public string name { get; set; }
    public Coord coord { get; set; }
    public string country { get; set; }
}

public class RootObject
{
    public string cod { get; set; }
    public double message { get; set; }
    public int cnt { get; set; }
    public List<Forecast> list { get; set; }
    public City city { get; set; }
}

public class UnixTimestampConverter : Newtonsoft.Json.JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(DateTime);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
            .AddSeconds((long)reader.Value);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}