如何在Flutter中为指定JSON创建Model类并访问sub_asset_class_weight
报错原因
- 自动生成工具报错的核心原因是
sub_asset_class_weight字段为嵌套异构数组:每个子数组的第一个元素是字符串类型、第二个是双精度浮点型,通用JSON转Dart工具默认会将数组识别为同类型元素,无法自动适配这种异构结构,因此生成无效代码。
完整Dart Model实现(空安全版本,无额外依赖)
class AllocationResponse { final String statusMessage; final String statusCode; final dynamic error; final FinResult? finResult; AllocationResponse({ required this.statusMessage, required this.statusCode, this.error, this.finResult, }); factory AllocationResponse.fromJson(Map<String, dynamic> json) { return AllocationResponse( statusMessage: json['statusMessage'] as String, statusCode: json['statusCode'] as String, error: json['error'], finResult: json['finResult'] != null ? FinResult.fromJson(json['finResult']) : null, ); } Map<String, dynamic> toJson() { return { 'statusMessage': statusMessage, 'statusCode': statusCode, 'error': error, 'finResult': finResult?.toJson(), }; } } class FinResult { final int count; final AllocationResult? result; final int start; FinResult({ required this.count, this.result, required this.start, }); factory FinResult.fromJson(Map<String, dynamic> json) { return FinResult( count: json['count'] as int, result: json['result'] != null ? AllocationResult.fromJson(json['result']) : null, start: json['start'] as int, ); } Map<String, dynamic> toJson() { return { 'count': count, 'result': result?.toJson(), 'start': start, }; } } class AllocationResult { final List<GeneratedAllocationItem>? generatedAllocation; final dynamic executedAllocation; AllocationResult({ this.generatedAllocation, this.executedAllocation, }); factory AllocationResult.fromJson(Map<String, dynamic> json) { final generatedAllocationList = json['generatedAllocation'] as List?; return AllocationResult( generatedAllocation: generatedAllocationList?.map((item) => GeneratedAllocationItem.fromJson(item)).toList(), executedAllocation: json['executedAllocation'], ); } Map<String, dynamic> toJson() { return { 'generatedAllocation': generatedAllocation?.map((item) => item.toJson()).toList(), 'executedAllocation': executedAllocation, }; } } class GeneratedAllocationItem { final List<MapEntry<String, double>> subAssetClassWeight; final double weight; final String asset; GeneratedAllocationItem({ required this.subAssetClassWeight, required this.weight, required this.asset, }); factory GeneratedAllocationItem.fromJson(Map<String, dynamic> json) { final subWeightList = json['sub_asset_class_weight'] as List; // 转换异构数组为MapEntry列表,方便后续访问 final processedSubWeight = subWeightList.map((subItem) { final subList = subItem as List; return MapEntry(subList[0] as String, subList[1] as double); }).toList(); return GeneratedAllocationItem( subAssetClassWeight: processedSubWeight, weight: json['weight'] as double, asset: json['asset'] as String, ); } Map<String, dynamic> toJson() { // 转换回原始JSON要求的嵌套数组结构 final originSubWeight = subAssetClassWeight.map((entry) => [entry.key, entry.value]).toList(); return { 'sub_asset_class_weight': originSubWeight, 'weight': weight, 'asset': asset, }; } }
访问sub_asset_class_weight的示例代码
// 示例:解析接口返回的JSON数据 Map<String, dynamic> jsonData = /* 你的接口JSON响应 */; final allocationResponse = AllocationResponse.fromJson(jsonData); // 遍历访问所有sub_asset_class_weight属性 if (allocationResponse.finResult?.result?.generatedAllocation != null) { for (var allocation in allocationResponse.finResult!.result!.generatedAllocation!) { print("当前资产大类:${allocation.asset},大类权重:${allocation.weight}"); for (var subWeight in allocation.subAssetClassWeight) { print("子资产类别:${subWeight.key},子资产权重:${subWeight.value}"); } } }
内容的提问来源于stack exchange,提问作者Pallavi Tapkir
相关产品推荐
相关产品推荐

