雅虎天气预报c#

时间:2016-06-08 07:34:52

标签: c# api yahoo weather

1 个答案:

答案 0 :(得分:0)

只能说Yahoo Weather JSON格式。结果将在query.results.channel.item.forecast。查看以下示例,该示例使用Newtonsoft.Json进行JSON解析:

dynamic jsonData = JObject.Parse(responseFromServer);
if (jsonData["query"].count == 0)
{
    throw new Exception("Failed to obtain forecast from the server.");
}

var convertFromFtoC = jsonData["query"].results.channel.units.temperature == "F";

foreach (var v in jsonData["query"].results.channel.item.forecast)
{
    var date = (DateTime)v.date;
    var tempLo = (int)v.low;
    var tempHi = (int)v.high;

    if (convertFromFtoC)
    {
        tempLo = TemperatureConverter.FahrenheitToCelcius(tempLo);
        tempHi = TemperatureConverter.FahrenheitToCelcius(tempHi);
    }

    dailyForecst.Add(new ForecastDataForTheDay { Date = date, TempLo = tempLo, TempHi = tempHi });
}