在Flutter中解析json响应时抛出错误

quhf5bfb  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(242)

我试图解析一个简单的JSON响应。
{"获取选项结果":[{"选项组":空,"选项组ID":0,"选项显示名称":"290/200 HP V8发动机","选装件代码":" 1008852 "},{"选项组":空,"选项组ID":0,"选项显示名称":"290/225 HP V8发动机","选装件代码":" 1007059 "},{"选项组":空,"选项组ID":0,"选项显示名称":"343/235 HP V8发动机","选装件代码":" 1008853 "},{"选项组":空,"选项组ID":0,"选项显示名称":" 343/280 HP V8发动机","选装件代码":" 1005857 "},{"选项组":空,"选项组ID":0,"选项显示名称":" 360/315 HP V8发动机","选装件代码":" 1008854 "},{"选项组":空,"选项组ID":0,"选项显示名称":" 390/315 HP V8发动机","选装件代码":" 1008054 "},{"选项组":空,"选项组ID":0,"选项显示名称":" 4速变速器","选项代码":" 1007678 "},{"选项组":空,"选项组ID":0,"选项显示名称":" 6缸发动机","选装件代码":" 1006735 "},{"选项组":空,"选项组ID":0,"选项显示名称":" AMX大坏包","选项代码":" 1005249 "},{"选项组":空,"选项组ID":0,"选项显示名称":" Amx/标枪SST Go Package 390","选项代码":" 1007251 "},{"选项组":空,"选项组ID":0,"选项显示名称":"工厂空调","选项代码":" 1006048 "},{"选项组":空,"选项组ID":0,"选项显示名称":" Javelin SST大坏包","选项代码":" 1008355 "}]}
下面是我的POJO类来解析上面的json。

class ClassicCarsOptionsModel {
  List<GetOptionsResult>? getOptionsResult;

  ClassicCarsOptionsModel({this.getOptionsResult});

  ClassicCarsOptionsModel.fromJson(Map<String, dynamic> json) {
    if (json['GetOptionsResult'] != null) {
      getOptionsResult = <GetOptionsResult>[];
      json['GetOptionsResult'].map((v) {
        getOptionsResult!.add(new GetOptionsResult.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.getOptionsResult != null) {
      data['GetOptionsResult'] =
          this.getOptionsResult!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class GetOptionsResult {
  String? optionGroup;
  int? optionGroupID;
  String? optionDisplayName;
  String? optionCode;

  GetOptionsResult(
      {this.optionGroup,
      this.optionGroupID,
      this.optionDisplayName,
      this.optionCode});

  GetOptionsResult.fromJson(Map<String, dynamic> json) {
    optionGroup = json['OptionGroup'];
    optionGroupID = json['OptionGroupID'];
    optionDisplayName = json['OptionDisplayName'];
    optionCode = json['OptionCode'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['OptionGroup'] = this.optionGroup;
    data['OptionGroupID'] = this.optionGroupID;
    data['OptionDisplayName'] = this.optionDisplayName;
    data['OptionCode'] = this.optionCode;
    return data;
  }
}

字符串
它抛出了一个错误,我无法解析数据。
我试着调试到POJO类,但它一直抛出一个错误?可能是什么问题?如何跟踪崩溃的确切位置和原因?

um6iljoc

um6iljoc1#

尝试在Catch块中打印异常,并在API请求期间查看堆栈跟踪。同样,如果你使用dio,那么就使用拦截器来跟踪请求和响应的活动。

相关问题