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

时间:2022-03-10 12:23:21 

今天用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; }

正确无误了。

标签:newtonsoft.json,json
0
投稿

猜你喜欢

  • AndroidApk混淆编译时,报告java.io.IOException...错误解决办法

    2021-06-10 13:54:38
  • 在spring中手写全局异常拦 截器

    2023-11-09 01:25:42
  • JAVA判断空值方法原理解析

    2021-09-28 14:37:34
  • 关于Java实现HttpServer模拟前端接口调用

    2021-07-10 07:44:17
  • Java使用Graphics2D绘制SVG和PNG的方法

    2021-11-13 01:03:51
  • 浅谈SpringMVC的执行流程

    2023-09-30 17:59:59
  • C#实现简单文本编辑器

    2022-04-28 06:42:30
  • Android recyclerview实现纵向虚线时间轴的示例代码

    2023-08-23 07:03:39
  • 在C#中使用Channels的完整教程

    2021-11-03 15:41:28
  • Android 音乐播放器的开发实例详解

    2023-12-07 17:53:10
  • 在SpringBoot项目中的使用Swagger的方法示例

    2022-01-04 15:14:39
  • Java包装类的概述与应用

    2022-03-14 07:50:43
  • Java 线程池原理深入分析

    2023-01-30 19:59:43
  • C#数据类型实现背包、队列和栈

    2022-10-07 06:42:09
  • spring boot动态切换数据源的实现

    2022-04-20 14:17:03
  • Spring Aop 如何获取参数名参数值

    2022-09-08 17:00:41
  • struts2中使用注解配置Action方法详解

    2023-08-30 00:01:25
  • 详解Spark Sql在UDF中如何引用外部数据

    2021-08-17 14:51:17
  • Java实现JDK动态代理的原理详解

    2021-09-19 08:17:43
  • Android实现Service重启的方法

    2021-10-01 09:25:25
  • asp之家 软件编程 m.aspxhome.com