【发布时间】:2019-05-03 16:06:26
【问题描述】:
我正在尝试解析来自https://www.worldweatheronline.com 的 api 的响应。
我得到了这些结果(由于帖子的字符限制,我将其缩短):
{{
"data": {
"request": [
{
"type": "LatLon",
"query": "Lat 33.41 and Lon -86.94"
}
],
"nearest_area": [
{
"areaName": [
{
"value": "Brickyard Junction"
}
],
"country": [
{
"value": "United States of America"
}
],
"region": [
{
"value": "Alabama"
}
],
"latitude": "33.410",
"longitude": "-86.942",
"population": "0",
"weatherUrl": [
{
"value": "http://api-cdn.worldweatheronline.com/v2/weather.aspx?q=33.408696,-86.937835"
}
]
}
],
"weather": [
{
"date": "2019-03-20",
"astronomy": [
{
"sunrise": "06:52 AM",
"sunset": "06:59 PM",
"moonrise": "06:47 PM",
"moonset": "06:51 AM",
"moon_phase": "Waxing Gibbous",
"moon_illumination": "97"
}
],
"maxtempC": "20",
"maxtempF": "69",
"mintempC": "8",
"mintempF": "46",
"totalSnow_cm": "0.0",
"sunHour": "11.6",
"uvIndex": "5",
"hourly": [
{
"time": "0",
"tempC": "9",
"tempF": "49",
"windspeedMiles": "4",
"windspeedKmph": "7",
"winddirDegree": "65",
"winddir16Point": "ENE",
"weatherCode": "113",
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png"
}
],
"weatherDesc": [
{
"value": "Clear"
}
],
"precipMM": "0.0",
"humidity": "53",
"visibility": "10",
"pressure": "1026",
"cloudcover": "2",
"HeatIndexC": "9",
"HeatIndexF": "49",
"DewPointC": "0",
"DewPointF": "33",
"WindChillC": "9",
"WindChillF": "47",
"WindGustMiles": "7",
"WindGustKmph": "11",
"FeelsLikeC": "9",
"FeelsLikeF": "47",
"uvIndex": "0"
},
{
"time": "100",
"tempC": "9",
"tempF": "49",
"windspeedMiles": "4",
"windspeedKmph": "6",
"winddirDegree": "70",
"winddir16Point": "ENE",
"weatherCode": "113",
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png"
}
],
"weatherDesc": [
{
"value": "Clear"
}
],
"precipMM": "0.0",
"humidity": "54",
"visibility": "10",
"pressure": "1026",
"cloudcover": "2",
"HeatIndexC": "9",
"HeatIndexF": "49",
"DewPointC": "0",
"DewPointF": "33",
"WindChillC": "9",
"WindChillF": "47",
"WindGustMiles": "6",
"WindGustKmph": "10",
"FeelsLikeC": "9",
"FeelsLikeF": "47",
"uvIndex": "0"
},
{
"time": "200",
"tempC": "9",
"tempF": "48",
"windspeedMiles": "4",
"windspeedKmph": "6",
"winddirDegree": "76",
"winddir16Point": "ENE",
"weatherCode": "116",
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
}
],
"weatherDesc": [
{
"value": "Partly cloudy"
}
],
"precipMM": "0.0",
"humidity": "55",
"visibility": "10",
"pressure": "1026",
"cloudcover": "1",
"HeatIndexC": "9",
"HeatIndexF": "48",
"DewPointC": "0",
"DewPointF": "33",
"WindChillC": "8",
"WindChillF": "47",
"WindGustMiles": "5",
"WindGustKmph": "9",
"FeelsLikeC": "8",
"FeelsLikeF": "47",
"uvIndex": "0"
},
{
"time": "300",
"tempC": "9",
"tempF": "48",
"windspeedMiles": "3",
"windspeedKmph": "5",
"winddirDegree": "82",
"winddir16Point": "E",
"weatherCode": "116",
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
}
],
"weatherDesc": [
{
"value": "Partly cloudy"
}
],
"precipMM": "0.0",
"humidity": "56",
"visibility": "10",
"pressure": "1025",
"cloudcover": "1",
"HeatIndexC": "9",
"HeatIndexF": "48",
"DewPointC": "0",
"DewPointF": "33",
"WindChillC": "8",
"WindChillF": "47",
"WindGustMiles": "5",
"WindGustKmph": "8",
"FeelsLikeC": "8",
"FeelsLikeF": "47",
"uvIndex": "0"
},
我正在尝试解析 json 结果并获取空值。
private async Task<List<HourData>> GetDataAsync()
{
try
{
var datas = new List<HourData>();
HttpResponseMessage response = await client.GetAsync(_url);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
JObject obj = JObject.Parse(content);
var token = obj.SelectToken("weather"); // *** NullReferenceException HERE
var tokenHours = (JArray) token.SelectToken("hourly");
foreach (var tk in tokenHours)
{
var json = JsonConvert.SerializeObject(tk);
datas.Add(JsonConvert.DeserializeObject<HourData>(json));
}
}
// return product;
return datas;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
public class HourData
{
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 Temperature { get; set; }
public string ApparentTemperature { get; set; }
public string DewPoint { get; set; }
public string Humidity { get; set; }
public string Pressure { get; set; }
public string windSpeed { get; set; }
public string windGust { get; set; }
public string windBearing { get; set; }
public string cloudCover { get; set; }
public string uvIndex { get; set; }
public string visibility { get; set; }
}
我在var token = obj.SelectToken("weather"); 这一行收到了NullReferenceException。
我也尝试了以下方法并得到了相同的结果:
var token = obj.SelectToken("data");
var tokenHours = (JArray) token.SelectToken("weather");
我需要将每小时数据放入我的类数组中,但它没有解析。
我是否在解析错误的单词?我不知道为什么它没有正确解析。
任何帮助将不胜感激!
【问题讨论】:
-
“空异常”是指
NullReferenceException吗? -
@Amy 就是这样。
-
还有其他种类的“空异常”。具体很重要。
-
如果您在该行特别收到
NullReferenceException,则可能是obj没有正确填充。您是否已介入调试器以确保JObject.Parse(content);成功? -
@FlutterDashie 感谢您的回复。我从解析中得到一个结果,上面发布的 json 就是结果。