Asp.net core 无法反序列化当前JSON对象无法使用组列反序列化当前JSON对象
我正在调用API并尝试将Jsonresult存储到IEnumerable模型类中。json结果具有组头列consolidated\u weather。 当我运行该程序时,出现以下错误 JsonSerializationException:无法将当前JSON对象反序列化为“System.Collections.Generic.IEnumerable”类型 在避免json结果中的组头之后,如何将给定的json结果调用到IEnumerable模型类中 我使用以下两种模型Asp.net core 无法反序列化当前JSON对象无法使用组列反序列化当前JSON对象,asp.net-core,asp.net-web-api,Asp.net Core,Asp.net Web Api,我正在调用API并尝试将Jsonresult存储到IEnumerable模型类中。json结果具有组头列consolidated\u weather。 当我运行该程序时,出现以下错误 JsonSerializationException:无法将当前JSON对象反序列化为“System.Collections.Generic.IEnumerable”类型 在避免json结果中的组头之后,如何将给定的json结果调用到IEnumerable模型类中 我使用以下两种模型 public class W
public class Weather
{
[Key]
public long id { get; set; }
public string weather_state_name { get; set; }
public string weather_state_abbr { get; set; }
public string wind_direction_compass { get; set; }
public DateTime created { get; set; }
public DateTime applicable_date { get; set; }
public decimal min_temp { get; set; }
public decimal max_temp { get; set; }
public decimal the_temp { get; set; }
public double wind_speed { get; set; }
public decimal wind_direction { get; set; }
public decimal air_pressure { get; set; }
public decimal Humidity { get; set; }
public string Visibllity { get; set; }
public decimal Predictability { get; set; }
}
public class WeatherList
{
public IEnumerable<Weather> consolidated_weather { get; set; }
}
public async Task<IEnumerable<Weather>> GetWeatherAsync(string woied)
{
var url = SD.APIBaseUrl + woied;
var request = new HttpRequestMessage(HttpMethod.Get, url);
var client = _clientFactory.CreateClient();
HttpResponseMessage response = await client.SendAsync(request);
IEnumerable<Weather> weather = new List<Weather>();
WeatherList weatherList = new WeatherList();
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var jsonString = await response.Content.ReadAsStringAsync();
weatherList = JsonConvert.DeserializeObject<WeatherList>(jsonString);
return weatherList;
}
return null;
}
公共类天气
{
[关键]
公共长id{get;set;}
公共字符串weather\u state\u name{get;set;}
公共字符串weather_state_abbr{get;set;}
公共字符串风向指南针{get;set;}
已创建公共日期时间{get;set;}
公共日期时间适用_日期{get;set;}
公共十进制最小值{get;set;}
公共十进制最大值{get;set;}
公共十进制参数{get;set;}
公共双风速{get;set;}
公共十进制风向{get;set;}
公共十进制空气压力{get;set;}
公共十进制湿度{get;set;}
公共字符串可见性{get;set;}
公共十进制可预测性{get;set;}
}
公共类天气表
{
公共IEnumerable合并_weather{get;set;}
}
公共异步任务GetWeatherAsync(字符串woied)
{
var url=SD.APIBaseUrl+woied;
var request=newhttprequestmessage(HttpMethod.Get,url);
var client=_clientFactory.CreateClient();
HttpResponseMessage response=等待客户端.SendAsync(请求);
IEnumerable weather=新列表();
天气表天气表=新天气表();
if(response.StatusCode==System.Net.HttpStatusCode.OK)
{
var jsonString=await response.Content.ReadAsStringAsync();
weatherList=JsonConvert.DeserializeObject(jsonString);
返回天气表;
}
返回null;
}
API的结果如下所示
<!-- language: lang-html -->
{
"consolidated_weather": [
{
"id": 4577625064341504,
"weather_state_name": "Heavy Rain",
"weather_state_abbr": "hr",
"wind_direction_compass": "WSW",
"created": "2020-07-14T19:35:14.577740Z",
"applicable_date": "2020-07-14",
"min_temp": 11.11,
"max_temp": 15.05,
"the_temp": 14.32,
"wind_speed": 6.570953330777592,
"wind_direction": 254.13274105640758,
"air_pressure": 1016.5,
"humidity": 85,
"visibility": 7.654361031575599,
"predictability": 77
},
{
"id": 4896540210495488,
"weather_state_name": "Showers",
"weather_state_abbr": "s",
"wind_direction_compass": "WNW",
"created": "2020-07-14T19:35:17.569534Z",
"applicable_date": "2020-07-15",
"min_temp": 12.31,
"max_temp": 17.03,
"the_temp": 16.509999999999998,
"wind_speed": 7.600821124862802,
"wind_direction": 284.49357944800784,
"air_pressure": 1015.5,
"humidity": 82,
"visibility": 13.558008729022509,
"predictability": 73
},
]
"title": "Texas",
"location_type": "City",
"timezone": "US"
<!-- end snippet -->
{
“综合天气”:[
{
“id”:4577625064341504,
“天气状态名称”:“大雨”,
“天气状况”:“hr”,
“风向罗盘”:“WSW”,
“已创建”:“2020-07-14T19:35:14.577740Z”,
“适用日期”:“2020-07-14”,
“最低温度”:11.11,
“最高温度”:15.05,
“温度”:14.32,
“风速”:6.57095333077592,
“风向”:254.13274105640758,
“空气压力”:1016.5,
“湿度”:85,
“能见度”:7.6543610315599,
“可预测性”:77
},
{
“id”:4896540210495488,
“天气状态名称”:“阵雨”,
“天气状况”:“s”,
“风向罗盘”:“西北风”,
“已创建”:“2020-07-14T19:35:17.569534Z”,
“适用日期”:“2020-07-15”,
“最低温度”:12.31,
“最高温度”:17.03,
“温度”:16.50999998,
“风速”:7.600821124862802,
“风向”:284.49357944800784,
“空气压力”:1015.5,
“湿度”:82,
“能见度”:13.558008729022509,
“可预测性”:73
},
]
“头衔”:“德克萨斯州”,
“地点类型”:“城市”,
“时区”:“美国”
假设您无法更改Json的内容,您可以创建一个包含天气列表的ConsolidatedWeather类
public class ConsolidatedWeather
{
public List<Weather> Consolidated_Weather { get; set; }
}
公共类合并天气
{
公共列表{get;set;}
}
在你的反序列化部分
return JsonConvert.DeserializeObject<ConsolidatedWeather>(jsonString).Consolidated_Weather;
返回JsonConvert.DeserializeObject(jsonString).Consolidated\u天气;
从您的json字符串中,我们可以看到objectconsolidated\u weather
中有一个weather
对象列表。
因此,您需要将json解析为WeatherList
public class WeatherList {
public IEnumerable<Weather> consolidated_weather { get; set; }
}
[HttpPost("/weather")]
public async Task<IEnumerable<Weather>> GetWeatherAsync()
{
string jsonString = "{\"consolidated_weather\":[{\"id\":4577625064341504,\"weather_state_name\":\"Heavy Rain\",\"weather_state_abbr\":\"hr\",\"wind_direction_compass\":\"WSW\",\"created\":\"2020-07-14T19:35:14.577740Z\",\"applicable_date\":\"2020-07-14\",\"min_temp\":11.11,\"max_temp\":15.05,\"the_temp\":14.32,\"wind_speed\":6.570953330777592,\"wind_direction\":254.13274105640758,\"air_pressure\":1016.5,\"humidity\":85,\"visibility\":7.654361031575599,\"predictability\":77},{\"id\":4896540210495488,\"weather_state_name\":\"Showers\",\"weather_state_abbr\":\"s\",\"wind_direction_compass\":\"WNW\",\"created\":\"2020-07-14T19:35:17.569534Z\",\"applicable_date\":\"2020-07-15\",\"min_temp\":12.31,\"max_temp\":17.03,\"the_temp\":16.509999999999998,\"wind_speed\":7.600821124862802,\"wind_direction\":284.49357944800784,\"air_pressure\":1015.5,\"humidity\":82,\"visibility\":13.558008729022509,\"predictability\":73}]}";
IEnumerable<Weather> weather = new List<Weather>();
WeatherList weatherList = new WeatherList();
weatherList = JsonConvert.DeserializeObject<WeatherList>(jsonString);
return weatherList.consolidated_weather;
}
我也这样做了,但仍然不起作用。出现错误消息“无法隐式转换类型system.collections.Generic.Ienumerable.Hi,您能更新您的代码吗?以便我能提供帮助。Michelle我忘了添加“Title”“Location type”,该列应该从字符串中忽略。请检查我更新的帖子,这是原因您没有很好地反序列化对象。它必须与json格式相同。将其他属性添加到WeatherList中,它就会很好地工作。
weather
变量从未使用过DHI,我认为是“null”可能与您的jsonString有关。您首先发布的jsonString尚未完成。您是故意缩短它还是整个字符串?我建议您可以先从我的代码中测试jsonString,以反序列化WeatherList对象。如果我们成功,那么您可以检查jsonString API。代码中存在多个问题。ThJSON字符串被截断,该方法返回IEnumerable
,但该代码返回单个WeatherList
,并且从未使用weather
变量。这甚至不会编译,更不用说产生异常了
public class WeatherList
{
public IEnumerable<Weather> consolidated_weather { get; set; }
public string title { get; set; }
public string location_type { get; set; }
public string timezone { get; set; }
}
<!-- language: lang-html -->
{
"consolidated_weather": [
{
"id": 4577625064341504,
...
},
{
"id": 4896540210495488,
...
},
] <!-- missing , -->
"title": "Texas",
"location_type": "City",
"timezone": "US"
<!-- missing } -->
<!-- end snippet -->