我是json的新手,正在尝试解析一些天气数据。除了我要尝试获取的一条信息,一切都很好。
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("data.weather");
var tokenHours = (JArray)token[0].SelectToken("hourly");
foreach (var tk in tokenHours)
{
var json = JsonConvert.SerializeObject(tk);
datas.Add(JsonConvert.DeserializeObject<HourData>(json));
}
}
public class HourData
{
public string weatherDesc { get; set; }
public string time { get; set; }
public string tempF { 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; }
public string windspeedMiles { get; set; }
}
有问题的json是这样的:
{
"data": {
"request": [
{
"type": "LatLon",
"query": "Lat 33.41 and Lon -86.94"
}
],
"nearest_area": [
{
"areaName": [
{
"value": "Bessemer Homestead"
}
],
"country": [
{
"value": "United States of America"
}
],
"region": [
{
"value": "Alabama"
}
],
"latitude": "33.404",
"longitude": "-86.939",
"population": "0",
"weatherUrl": [
{
"value": "http://api-cdn.worldweatheronline.com/v2/weather.aspx?q=33.4063059,-86.9385553"
}
]
}
],
"weather": [
{
"date": "2019-04-20",
"astronomy": [
{
"sunrise": "06:12 AM",
"sunset": "07:22 PM",
"moonrise": "08:54 PM",
"moonset": "07:15 AM",
"moon_phase": "Waxing Gibbous",
"moon_illumination": "88"
}
],
"maxtempC": "16",
"maxtempF": "61",
"mintempC": "7",
"mintempF": "45",
"totalSnow_cm": "0.0",
"sunHour": "9.1",
"uvIndex": "3",
"hourly": [
{
"time": "0",
"tempC": "7",
"tempF": "44",
"windspeedMiles": "11",
"windspeedKmph": "17",
"winddirDegree": "278",
"winddir16Point": "W",
"weatherCode": "122",
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
}
],
"weatherDesc": [
{
"value": "Overcast"
}
],
"precipMM": "0.1",
"humidity": "84",
"visibility": "5",
"pressure": "1009",
"cloudcover": "100",
"HeatIndexC": "7",
"HeatIndexF": "44",
"DewPointC": "4",
"DewPointF": "40",
"WindChillC": "4",
"WindChillF": "39",
"WindGustMiles": "15",
"WindGustKmph": "24",
"FeelsLikeC": "4",
"FeelsLikeF": "39",
"uvIndex": "0"
我正在尝试访问json的这一部分:
"weatherDesc": [
{
"value": "Overcast"
}
将weatherDesc添加到HourData时,出现此异常:
Newtonsoft.Json.JsonReaderException:读取字符串时出错。意外 令牌:StartArray。路径“ weatherDesc”,第1行,位置280。在 Newtonsoft.Json.JsonReader.ReadAsStringInternal()在 Newtonsoft.Json.JsonTextReader.ReadAsString()在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader 阅读器,JsonContract合同,布尔hasConverter)位于 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader阅读器,JsonObjectContract合同, JsonProperty成员,字符串ID)位于 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader,类型objectType,JsonContract合同,JsonProperty成员, JsonContainerContract containerContract,JsonProperty containerMember, 对象的现存值) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader,类型objectType,JsonContract合同,JsonProperty成员, JsonContainerContract containerContract,JsonProperty containerMember, 对象的现存值) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader 阅读器,输入objectType,布尔值checkAdditionalContent) Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, 在以下位置输入objectType) Newtonsoft.Json.JsonConvert.DeserializeObject(String value,Type type, JsonSerializerSettings设置)位于 Newtonsoft.Json.JsonConvert.DeserializeObject [T](String value, JsonSerializerSettings设置)位于 Newtonsoft.Json.JsonConvert.DeserializeObject [T](String value)在 SuburbanWebService.ddays.apis.DarkskyApiReader.d__6.MoveNext() 在D:\ Source \ techguy \ Suburban Test中 项目\ DarkSkyTest \ DarkSkyTest \ DarkskyApiReader.cs:第151行一个或 发生了更多错误。
除weatherDesc之外的所有内容都可以解析。我得到的想法是它没有介入json的那部分,至少这是我的理解。如何进入weatherDesc并将数据输入我的班级?
任何帮助将不胜感激!
答案 0 :(得分:2)
创建一个类来保存weatherDesc
的数据:
public class WeatherDesc
{
public string Value { get; set; }
}
然后在您的HourData
类中使用它:
public class HourData
{
public IEnumerable<WeatherDesc> weatherDesc { get; set; }
...
}
根据错误,它正在读取数组,因此为什么IEnumerable
。
答案 1 :(得分:0)
主要问题在于,weatherDesc
实际上是一个数组,而不是单个字符串。您应该能够通过将weatherDesc
字段设为string[]
而不是string
来解决此问题。如果要将单例(仅包含1个条目的集合)数组展平为一个字符串,则必须自己对该对象进行反序列化。