【问题标题】:Trouble parsing forecast.io weather data with Json.Net使用 Json.Net 解析 forecast.io 天气数据时遇到问题
【发布时间】:2016-05-21 19:17:04
【问题描述】:

我对 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; }
}

【问题讨论】:

  • 我建议您搜索 Newtonsoft JSON 并查看示例以了解如何使用它。
  • 1) 您在问题中包含的 JSON 不完整。它至少应该包括外括号。您可以编辑您的问题以包含完整的 JSON 吗? 2)尝试解析JSON后,你有什么问题?

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


【解决方案1】:

首先,您发布的 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,那么您的类应该是这样的。特别要注意DailyWeatherData 类的data 属性定义为项目列表(不是单个对象),它对应于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

【讨论】:

  • 没问题;很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
相关资源
最近更新 更多