newtonsoft.json解析天气数据出错解决方法

时间:2021-05-19

今天用NewtonSoft.JSon解析一个天气数据,数据格式如:
复制代码 代码如下:
{"status":1,"detail":"\u6570\u636e\u83b7\u53d6\u6210\u529f","data":[[{"date":"2014-01-01","dis_id":"1119","dis_name":"\u5f90\u5dde\u5e02","url":"http:\/\/\/local.php?dis_id=1119","weather":"1","wind":"\u5317\u98ce3-4\u7ea7","weather_name":"\u6674","weather_pic":"styles\/images\/icon2\/night\/1.png","humidity":"","tem":"1"}]}]]}

结果就老报一个错误。

复制代码 代码如下:
Cannot deserialize JSON array (i.e. [1,2,3]) into type 'SweetWeather.MoreDayWeatherInfoFullDay'.
The deserialized type must be an array or implement a collection interface like IEnumerable, ICollection or IList.
To force JSON arrays to deserialize add the JsonArrayAttribute to the type. Path 'data[0]', line 1, position 69.

最后还是报错。仔细发现有两个方框号,原来是自己实体类的设计有问题,

原实体类:

复制代码 代码如下:
public string status { get; set; }
public string detail { get; set; }
public List<MoreDayWeatherInfoFullDay> data { get; set;

更正后的实体类:

复制代码 代码如下:
public string status { get; set; }
public string detail { get; set; }
public List<List<MoreDayWeatherInfoFullDay>> data { get; set; }

正确无误了。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章