newtonsoft.json解析天气数据出错解决方法
程序员文章站
2024-02-24 16:00:28
今天用newtonsoft.json解析一个天气数据,数据格式如:复制代码 代码如下:{"status":1,"detail":"\u6570\u636e\u83b7\u5...
今天用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:\/\/www.tianqiyubao.com\/local.php?dis_id=1119","weather":"1","wind":"\u897f\u98ce3-4\u7ea7","weather_name":"\u6674","weather_pic":"styles\/images\/icon2\/day\/1.png","humidity":"","tem_min":"1","tem_max":"13","sunrise":"07:17","sunset":"17:12","pm":"109","air":"\u826f","day_night":[{"date":"2014-01-01","dis_id":"1119","dis_name":"\u5f90\u5dde\u5e02","url":"http:\/\/www.tianqiyubao.com\/local.php?dis_id=1119","weather":"1","wind":"\u897f\u98ce3-4\u7ea7","weather_name":"\u6674","weather_pic":"styles\/images\/icon2\/day\/1.png","humidity":"","tem":"13"},{"date":"2014-01-01","dis_id":"1119","dis_name":"\u5f90\u5dde\u5e02","url":"http:\/\/www.tianqiyubao.com\/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; }
正确无误了。