【发布时间】:2019-07-31 18:55:52
【问题描述】:
从网站metcheck我可以得到一个天气预报的json文件。 http://ws1.metcheck.com/ENGINE/v9_0/json.asp?lat=52.380&lon=0.060&Fc=Av
文件回来了,但我无法读取它以获取我需要的信息。我想我不明白metcheckData、forecastLocation和forecast之间的关系:
{
"metcheckData":{
"forecastLocation":{
"forecast":[
{
"temperature":"18",
"dewpoint":"13",
"rain":"0.0",
"freezinglevel":"3049",
"uvIndex":"1",
"totalcloud":"65",
"lowcloud":"55",
"medcloud":"43",
"highcloud":"11",
"humidity":"79",
"windspeed":"11",
"meansealevelpressure":"1012.77",
"windgustspeed":"17",
"winddirection":"249",
"windletter":"WSW",
"icon":"PC",
"iconName":"Partly Cloudy",
"chanceofrain":"0",
"chanceofsnow":"0",
"dayOfWeek":"4",
"weekday":"Wednesday",
"sunrise":"6:02",
"sunset":"18:09",
"cumulusBaseHeight":"540",
"stratusBaseHeight":"549",
"dayOrNight":"N",
"utcTime":"2019-07-31T19:00:00.00"
},
{
"temperature":"17",
"dewpoint":"13",
"rain":"0.1",
"freezinglevel":"3192",
"uvIndex":"0",
"totalcloud":"91",
"lowcloud":"66",
"medcloud":"39",
"highcloud":"35",
"humidity":"82",
"windspeed":"11",
"meansealevelpressure":"1013.29",
"windgustspeed":"17",
"winddirection":"245",
"windletter":"WSW",
"icon":"RO",
"iconName":"Intermittent Rain",
"chanceofrain":"47",
"chanceofsnow":"0",
"dayOfWeek":"4",
"weekday":"Wednesday",
"sunrise":"6:02",
"sunset":"18:09",
"cumulusBaseHeight":"512",
"stratusBaseHeight":"520",
"dayOrNight":"N",
"utcTime":"2019-07-31T20:00:00.00"
}
],
"continent":"",
"country":"",
"location":"52.4/0.1",
"latitude":52.4,
"longitude":0.1,
"timezone":0
}
// Many other similar array entries omitted
},
"feedCreation":"2019-07-31T20:26:10.00",
"feedCreator":"Metcheck.com",
"feedModel":"GHX5",
"feedModelRun":"00Z",
"feedModelRunInitialTime":"2019-07-31T00:00:00.00",
"feedResolution":"0.01"
}
使用 使用 Newtonsoft.Json; 使用 Newtonsoft.Json.Linq;
我尝试使用以下代码读取特定时间的温度预测等内容。
JObject jo = JObject.Parse(File.ReadAllText(@"C:\temp\Weather.json", Encoding.UTF8));
Dictionary<string, List<string>> values =
jo.SelectToken("forecast", true).ToObject<Dictionary<string, List<string>>>();
foreach (var kv in values)
{
rchtxtbx_output.AppendText(kv.Value[0] + "\r");
以此类推,认为 kv.Value[0] 将是温度,我将四处走动并获取每小时的温度。不幸的是,事实并非如此,我在
处遇到错误Dictionary<string, List<string>> values =
jo.SelectToken("forecast", true).ToObject<Dictionary<string, List<string>>>();
所以有些“预测”是不正确的。我也尝试过metcheckData.forecastLocation.forecast,然后是forecastLocation.forecast,但都有错误。
请告诉我如何从 json 文件中获取数据并为每个小时的预测写入富文本框。
【问题讨论】:
-
我使用jsonformatter.curiousconcept.com 清理了您的 JSON 格式。如果我有任何问题,请随时恢复更改。