C# 无法使用Json.Net解析forecast.io天气数据
我对JSON比较陌生,使用APIC# 无法使用Json.Net解析forecast.io天气数据,c#,json,asp.net-mvc,json.net,C#,Json,Asp.net Mvc,Json.net,我对JSON比较陌生,使用APIforecast.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":"cle
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/“+纬度+”,“+经度);
var json=JObject.Parse(测试);
var daily=json.ToObject();
公共类每日天气预报
{
公共DailyWeatherData每日{get;set;}
}
公共类每日天气数据
{
公共每日数据{get;set;}
}
公共课日报
{
公共字符串时间{get;set;}
公共字符串摘要{get;set;}
公共字符串图标{get;set;}
公共字符串精度{get;set;}
公共字符串的精确性{get;set;}
公共字符串时间{get;set;}
公共字符串sunsetTime{get;set;}
公共字符串{get;set;}
公共字符串precipIntensityMax{get;set;}
公共字符串temperatureMin{get;set;}
公共字符串temperatureMinTime{get;set;}
公共字符串temperatureMax{get;set;}
公共字符串temperatureMaxTime{get;set;}
公共字符串apparentTemperatureMin{get;set;}
公共字符串apparentTemperatureMinTime{get;set;}
公共字符串apparentTemperatureMax{get;set;}
公共字符串apparentTemperatureMaxTime{get;set;}
公共字符串dewPoint{get;set;}
公共字符串{get;set;}
公共字符串windSpeed{get;set;}
公共字符串风向标{get;set;}
公共字符串cloudCover{get;set;}
公用管柱压力{get;set;}
公共字符串{get;set;}
}
首先,您发布的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; }
}
public类DailyWeatherDTO//根级容器对象
{
公共DailyWeatherData每日{get;set;}
}
公共类每日天气数据
{
公共字符串摘要{get;set;}
公共字符串图标{get;set;}
公共列表数据{get;set;}
}
公共类气象项目
{
公共整数时间{get;set;}
公共字符串摘要{get;set;}
公共字符串图标{get;set;}
公共时间{get;set;}
公共int sunsetTime{get;set;}
公共双相位{get;set;}
公共整数精度{get;set;}
公共整数precipIntensityMax{get;set;}
公共整数精度可能性{get;set;}
公共双温{get;set;}
公共int温度时间{get;set;}
公共双温度max{get;set;}
public int temperatureMaxTime{get;set;}
公共双表温度{get;set;}
public int apparentTemperatureMinTime{get;set;}
公共双表温度max{get;set;}
public int apparentTemperatureMaxTime{get;set;}
公共双露点{get;set;}
公共双湿度{get;set;}
公共双风速{get;set;}
公共int风向标{get;set;}
public int cloudCover{get;set;}
公共双重压力{get;set;}
公共双精度{get;set;}
}
使用此类结构,您可以按如下方式反序列化JSON:
DailyWeatherDTO dto = JsonConvert.DeserializeObject<DailyWeatherDTO>(json);
dailyWeatherdToDTO=JsonConvert.DeserializeObject(json);
这里是一个演示:我建议您搜索Newtonsoft JSON,并查看示例以了解如何使用它。1)您在问题中包含的JSON不完整。它至少应包括外部支架。您可以编辑您的问题以包含完整的JSON吗?2) 在尝试解析JSON之后,您遇到了什么问题?没有问题;很高兴我能帮忙。
DailyWeatherDTO dto = JsonConvert.DeserializeObject<DailyWeatherDTO>(json);