import 'dart:core'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/18 11:39 /// @description:自动判断 JSON 是单对象 还是 List,并解析 /// model_parser.dart class ModelParser { /// 自动解析 JSON: /// - 如果是 Map,就解析成 T /// - 如果是 List,就解析成 List static List parseList( dynamic json, T Function(Map) fromJson, ) { if (json is List) { return json.map((e) => fromJson(e as Map)).toList(); } else { throw Exception("预期是 List,但实际是 ${json.runtimeType}"); } } static T parseObject( dynamic json, T Function(Map) fromJson, ) { if (json is Map) { return fromJson(json); } else { throw Exception("预期是 Map,但实际是 ${json.runtimeType}"); } } }