无法使用Json.Net解析forecast.io天气数据

时间:2016-05-21 19:17:04

标签: c# json asp.net-mvc json.net

我对JSON相对较新并使用API​​ forecast.io。它返回下面的JSON,我需要解析它。

"daily":{
    "summary":"Drizzle on Monday and Tuesday, with temperatures bottoming out at 91°F on Monday.","icon":"rain",
"data":[{
    "time":1463770800,"summary":"Clear throughout the day.","icon":"clearday","sunriseTime":1463788956,"sunsetTime":1463839653,"moonPhase":0.48,"precipIntensity":0,"precipIntensityMax":0,"precipProbability":0,"temperatureMin":63.06,"temperatureMinTime":1463785200,"temperatureMax":95.23,"temperatureMaxTime":1463824800,"apparentTemperatureMin":63.06,"apparentTemperatureMinTime":1463785200,"apparentTemperatureMax":90.3,"apparentTemperatureMaxTime":1463824800,"dewPoint":37.34,"humidity":0.25,"windSpeed":3.44,"windBearing":22,"cloudCover":0,"pressure":1002.14,"ozone":283.7}

我每天提取""部分成功,但我无法获得"数据"在"每日"内。我需要有详细信息,即摘要,时间,图标等。我真的很感激一些帮助。

这是我的C#代码:

var test = new System.Net.WebClient().DownloadString("https://api.forecast.io/forecast/f2857958690caafc67d0dfba402c1f57/" + Latitude + "," + Longitude);

var json = JObject.Parse(test);
var daily = json.ToObject<DailyWeatherDTO>();

public class DailyWeatherDTO
{
    public DailyWeatherData daily { get; set; }
}

public class DailyWeatherData
{
    public daily data { get; set; }
}

public class daily
{
    public string time { get; set; }
    public String summary { get; set; }
    public String icon { get; set; }
    public String precipIntensity { get; set; }
    public String precipProbability { get; set; }
    public string sunriseTime { get; set; }
    public string sunsetTime { get; set; }
    public string moonPhase { get; set; }
    public string precipIntensityMax { get; set; }
    public string temperatureMin { get; set; }
    public string temperatureMinTime { get; set; }
    public string temperatureMax { get; set; }
    public string temperatureMaxTime { get; set; }
    public string apparentTemperatureMin { get; set; }
    public string apparentTemperatureMinTime { get; set; }
    public string apparentTemperatureMax { get; set; }
    public string apparentTemperatureMaxTime { get; set; }
    public string dewPoint { get; set; }
    public string humidity { get; set; }
    public string windSpeed { get; set; }
    public string windBearing { get; set; }
    public string cloudCover { get; set; }
    public string pressure { get; set; }
    public string ozone { get; set; }
}

1 个答案:

答案 0 :(得分:3)

首先,您发布的JSON无效,如图所示。具体来说,你最后错过了一个结束的方括号和大括号;另外,整件事需要用另一对花括号括起来才有效。以下是更正后的版本,为了便于阅读而重新格式化:

{
  "daily": {
    "summary": "Drizzle on Monday and Tuesday, with temperatures bottoming out at 91°F on Monday.",
    "icon": "rain",
    "data": [
      {
        "time": 1463770800,
        "summary": "Clear throughout the day.",
        "icon": "clearday",
        "sunriseTime": 1463788956,
        "sunsetTime": 1463839653,
        "moonPhase": 0.48,
        "precipIntensity": 0,
        "precipIntensityMax": 0,
        "precipProbability": 0,
        "temperatureMin": 63.06,
        "temperatureMinTime": 1463785200,
        "temperatureMax": 95.23,
        "temperatureMaxTime": 1463824800,
        "apparentTemperatureMin": 63.06,
        "apparentTemperatureMinTime": 1463785200,
        "apparentTemperatureMax": 90.3,
        "apparentTemperatureMaxTime": 1463824800,
        "dewPoint": 37.34,
        "humidity": 0.25,
        "windSpeed": 3.44,
        "windBearing": 22,
        "cloudCover": 0,
        "pressure": 1002.14,
        "ozone": 283.7
      }
    ]
  }
}

为了能够获取所有数据,您的类结构需要与JSON的结构相匹配。假设上面代表完整的JSON,这就是你的类应该是什么样子。特别要注意data类的DailyWeatherData属性被定义为项目列表(不是单个对象),它对应于JSON中data属性的方括号

public class DailyWeatherDTO  // root-level container object
{
    public DailyWeatherData daily { get; set; }
}

public class DailyWeatherData
{
    public string summary { get; set; }
    public string icon { get; set; }
    public List<WeatherItem> data { get; set; }
}

public class WeatherItem
{
    public int time { get; set; }
    public string summary { get; set; }
    public string icon { get; set; }
    public int sunriseTime { get; set; }
    public int sunsetTime { get; set; }
    public double moonPhase { get; set; }
    public int precipIntensity { get; set; }
    public int precipIntensityMax { get; set; }
    public int precipProbability { get; set; }
    public double temperatureMin { get; set; }
    public int temperatureMinTime { get; set; }
    public double temperatureMax { get; set; }
    public int temperatureMaxTime { get; set; }
    public double apparentTemperatureMin { get; set; }
    public int apparentTemperatureMinTime { get; set; }
    public double apparentTemperatureMax { get; set; }
    public int apparentTemperatureMaxTime { get; set; }
    public double dewPoint { get; set; }
    public double humidity { get; set; }
    public double windSpeed { get; set; }
    public int windBearing { get; set; }
    public int cloudCover { get; set; }
    public double pressure { get; set; }
    public double ozone { get; set; }
}

使用此类结构,您可以像这样反序列化JSON:

DailyWeatherDTO dto = JsonConvert.DeserializeObject<DailyWeatherDTO>(json);

以下是演示:https://dotnetfiddle.net/YTBrac