2016-05-21 40 views
1

我对JSON相对较新,并且使用API​​ forecast.io。它返回下面的JSON,我需要解析它。使用Json.Net解析forecast.io气象数据时遇到问题

"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; } 
} 
+0

我建议你搜索Newtonsoft JSON和看的例子,看看你如何使用它。 –

+0

1)您在问题中包含的JSON不完整。它应该至少包括外括号。你可以编辑你的问题,包括完整的JSON? 2)试图解析JSON后,你有什么问题? – dbc

回答

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,这里是你的类应该看起来像什么。尤其要注意的是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

+0

非常感谢你,大脑,它的作品! –

+0

没问题;很高兴我能帮上忙。 –

相关问题