Я пытаюсь проанализировать ответ от API в https://www.worldweatheronline.com.
Я получаю эти результаты (я сократил его из-за ограничения по количеству сообщений):
{{
"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; }
}
Я получаю NullReferenceException
на этой линии var token = obj.SelectToken("weather");
.
Я также попробовал следующее и получил те же результаты:
var token = obj.SelectToken("data");
var tokenHours = (JArray) token.SelectToken("weather");
Мне нужно вносить почасовые данные в мой массив классов, но это не анализ.
Я разбираю не то слово? Я заблудился из-за того, почему он не разбирается правильно.
Любая помощь будет принята с благодарностью!