Flutter中如何从API返回的对象数组获取最新dateoflastinsp日期
在Flutter中从JSON数组获取最新的dateoflastinsp日期
你在Flutter项目中从服务器拿到了包含巡检记录的JSON数据,需要从rows数组里提取出最新的dateoflastinsp日期,具体实现可以按以下步骤来:
首先,服务器返回的JSON数据如下:
{"drainageid": "","datacount": 98359,"resultcount": 98359,"page": "","rows": [{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": null,"dateofinsp": "2001-10-10T00:00:00","inspby": "SAW/ZAG"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2001-10-10T00:00:00","dateofinsp": "2002-09-24T00:00:00","inspby": "SAW+NBN"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2002-09-24T00:00:00","dateofinsp": "2003-09-18T00:00:00","inspby": "SAW/NBN"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2003-09-18T00:00:00","dateofinsp": "2004-09-08T00:00:00","inspby": "SAW/NBN"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2004-09-08T00:00:00","dateofinsp": "2005-05-05T00:00:00","inspby": "MZB/ASMG"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2005-05-05T00:00:00","dateofinsp": "2006-05-04T00:00:00","inspby": "MZB/ASZ"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2006-05-04T00:00:00","dateofinsp": "2007-05-09T00:00:00","inspby": "Ahmad/Haizul"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2007-05-09T00:00:00","dateofinsp": "2008-05-07T00:00:00","inspby": "Harzazi/Zulkornain"}]}
实现步骤
- 解析JSON数据:用
dart:convert里的jsonDecode把服务器返回的字符串转成Map对象,再提取出rows列表。 - 过滤无效数据:排除
dateoflastinsp为null的条目,避免后续转换出错。 - 转换日期格式:把字符串类型的日期转成
DateTime对象,这样才能进行日期比较。 - 筛选最新日期:遍历转换后的日期列表,找出最大的那个(也就是最新的日期),再按需转回字符串格式。
完整代码示例
import 'dart:convert'; void main() { // 服务器返回的JSON字符串 String jsonStr = '{"drainageid": "","datacount": 98359,"resultcount": 98359,"page": "","rows": [{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": null,"dateofinsp": "2001-10-10T00:00:00","inspby": "SAW/ZAG"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2001-10-10T00:00:00","dateofinsp": "2002-09-24T00:00:00","inspby": "SAW+NBN"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2002-09-24T00:00:00","dateofinsp": "2003-09-18T00:00:00","inspby": "SAW/NBN"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2003-09-18T00:00:00","dateofinsp": "2004-09-08T00:00:00","inspby": "SAW/NBN"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2004-09-08T00:00:00","dateofinsp": "2005-05-05T00:00:00","inspby": "MZB/ASMG"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2005-05-05T00:00:00","dateofinsp": "2006-05-04T00:00:00","inspby": "MZB/ASZ"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2006-05-04T00:00:00","dateofinsp": "2007-05-09T00:00:00","inspby": "Ahmad/Haizul"},{"id": "DR/C1/IC/H/-/GPG/PU/-/RCP/01","dateoflastinsp": "2007-05-09T00:00:00","dateofinsp": "2008-05-07T00:00:00","inspby": "Harzazi/Zulkornain"}]}'; // 解析JSON Map<String, dynamic> data = jsonDecode(jsonStr); List<dynamic> rows = data['rows']; // 过滤并转换日期 List<DateTime> validDates = rows .where((row) => row['dateoflastinsp'] != null) .map((row) => DateTime.parse(row['dateoflastinsp'])) .toList(); // 处理空列表情况 if (validDates.isEmpty) { print('没有有效的dateoflastinsp日期'); return; } // 找出最新日期 DateTime latestDate = validDates.reduce((a, b) => a.isAfter(b) ? a : b); // 输出结果,可按需转成字符串格式 print('最新的dateoflastinsp日期:${latestDate.toIso8601String()}'); }
注意事项
- 如果服务器返回的日期格式不是标准ISO8601,需要用
intl包的DateFormat来解析,不过你的示例里是标准格式,直接用DateTime.parse就行。 - 一定要处理
validDates为空的情况,避免调用reduce时抛出异常。
内容的提问来源于stack exchange,提问作者user21497668
相关产品推荐
相关产品推荐

