英文:
Flutter parsed nested list of object
问题
如何解析此JSON响应
{
"name_of_data_dummy_one": false,
"name_of_data_dummy_two": "1",
"name_of_data_dummy_three": 1,
"name_of_data": [
{
"name_of_data_one": "bla bla bla",
"name_of_data_two": "bla bla bla",
"name_of_data_three": "bla bla bla",
"name_of_data_four": "bla bla bla",
"name_of_data_five": "bla bla bla",
"name_of_data_six": "bla bla bla",
"name_of_data_seven": "bla bla bla",
"name_of_data_eight": "bla bla bla",
"name_of_data_nine": "bla bla bla",
"name_of_data_ten": "bla bla bla"
},
{
"name_of_data_one": "bla bla bla",
"name_of_data_two": "bla bla bla",
"name_of_data_three": "bla bla bla",
"name_of_data_four": "bla bla bla",
"name_of_data_five": "bla bla bla",
"name_of_data_six": "bla bla bla",
"name_of_data_seven": "bla bla bla",
"name_of_data_eight": "bla bla bla",
"name_of_data_nine": "bla bla bla",
"name_of_data_ten": "bla bla bla"
}
]
}
这是用于访问API的代码
Future<Either<String, List<CompanyListResponseModel>>> getCompanyList(
GetListCompanyModel companyListModel) async {
final response = await http.post(
Uri.parse('URL'),
body: thisIsModel.toJson(),
);
if (response.statusCode == 200) {
return Right(List<CompanyListResponseModel>.from(jsonDecode(response.body)
.map((x) => CompanyListResponseModel.fromJson(x))));
} else {
return const Left('get product error');
}
}
我已经生成了类似 quicktype.io 示例的响应数据,如下所示
class CompanyListResponseModel {
bool? nameOfDataDummyOne;
String? nameOfDataDummyTwo;
int? nameOfDataDummyThree;
List<NameOfDatum>? nameOfData;
CompanyListResponseModel({
this.nameOfDataDummyOne,
this.nameOfDataDummyTwo,
this.nameOfDataDummyThree,
this.nameOfData,
});
factory CompanyListResponseModel.fromRawJson(String str) =>
CompanyListResponseModel.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory CompanyListResponseModel.fromJson(Map<String, dynamic> json) =>
CompanyListResponseModel(
nameOfDataDummyOne: json["name_of_data_dummy_one"],
nameOfDataDummyTwo: json["name_of_data_dummy_two"],
nameOfDataDummyThree: json["name_of_data_dummy_three"],
nameOfData: json["name_of_data"] == null ? [] :
List<NameOfDatum>.from(json["name_of_data"].map((x) =>
NameOfDatum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"name_of_data_dummy_one": nameOfDataDummyOne,
"name_of_data_dummy_two": nameOfDataDummyTwo,
"name_of_data_dummy_three": nameOfDataDummyThree,
"name_of_data": nameOfData == null ? [] :
List<dynamic>.from(nameOfData!.map((x) => x.toJson())),
};
}
class NameOfDatum {
String? nameOfDataOne;
String? nameOfDataTwo;
String? nameOfDataThree;
String? nameOfDataFour;
String? nameOfDataFive;
String? nameOfDataSix;
String? nameOfDataSeven;
String? nameOfDataEight;
String? nameOfDataNine;
String? nameOfDataTen;
NameOfDatum({
this.nameOfDataOne,
this.nameOfDataTwo,
this.nameOfDataThree,
this.nameOfDataFour,
this.nameOfDataFive,
this.nameOfDataSix,
this.nameOfDataSeven,
this.nameOfDataEight,
this.nameOfDataNine,
this.nameOfDataTen,
});
factory NameOfDatum.fromRawJson(String str) =>
NameOfDatum.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory NameOfDatum.fromJson(Map<String, dynamic> json) =>
NameOfDatum(
nameOfDataOne: json["name_of_data_one"],
nameOfDataTwo: json["name_of_data_two"],
nameOfDataThree: json["name_of_data_three"],
nameOfDataFour: json["name_of_data_four"],
nameOfDataFive: json["name_of_data_five"],
nameOfDataSix: json["name_of_data_six"],
nameOfDataSeven: json["name_of_data_seven"],
nameOfDataEight: json["name_of_data_eight"],
nameOfDataNine: json["name_of_data_nine"],
nameOfDataTen: json["name_of_data_ten"],
);
Map<String, dynamic> toJson() => {
"name_of_data_one": nameOfDataOne,
"name_of_data_two": nameOfDataTwo,
"name_of_data_three": nameOfDataThree,
"name_of_data_four": nameOfDataFour,
"name_of_data_five": nameOfDataFive,
"name_of_data_six": nameOfDataSix,
"name_of_data_seven": nameOfDataSeven,
"name_of_data_eight": nameOfDataEight,
"name_of_data_nine": nameOfDataNine,
"name_of_data_ten": nameOfDataTen,
};
}
我遇到了这个错误 "Unhandled Exception: type 'List
我的期望是创建一个ListView,按照示例中的方式解析数据,顺便说一下,我使用了bloc状态管理。
英文:
How to parsed this response of json
{
"name_of_data_dummy_one": false,
"name_of_data_dummy_two": "1",
"name_of_data_dummy_three": 1,
"name_of_data": [
{
"name_of_data_one": "bla bla bla",
"name_of_data_two": "bla bla bla",
"name_of_data_three": "bla bla bla",
"name_of_data_four": "bla bla bla",
"name_of_data_five": "bla bla bla",
"name_of_data_six": "bla bla bla",
"name_of_data_seven": "bla bla bla",
"name_of_data_eight": "bla bla bla",
"name_of_data_nine": "bla bla bla",
"name_of_data_ten": "bla bla bla"
},
{
"name_of_data_one": "bla bla bla",
"name_of_data_two": "bla bla bla",
"name_of_data_three": "bla bla bla",
"name_of_data_four": "bla bla bla",
"name_of_data_five": "bla bla bla",
"name_of_data_six": "bla bla bla",
"name_of_data_seven": "bla bla bla",
"name_of_data_eight": "bla bla bla",
"name_of_data_nine": "bla bla bla",
"name_of_data_ten": "bla bla bla"
}
]
}
And heres the code for access the api
Future<Either<String, List<CompanyListResponseModel>>> getCompanyList(
GetListCompanyModel companyListModel) async {
final response = await http.post(
Uri.parse('URL'),
body: thisIsModel.toJson(),
);
if (response.statusCode == 200) {
return Right(List<CompanyListResponseModel>.from(jsonDecode(response.body)
.map((x) => CompanyListResponseModel.fromJson(x))));
} else {
return const Left('get product error');
}
}
I have generated response data like the example in quicktype.io loks like this
class CompanyListResponseModel {
bool? nameOfDataDummyOne;
String? nameOfDataDummyTwo;
int? nameOfDataDummyThree;
List<NameOfDatum>? nameOfData;
CompanyListResponseModel({
this.nameOfDataDummyOne,
this.nameOfDataDummyTwo,
this.nameOfDataDummyThree,
this.nameOfData,
});
factory CompanyListResponseModel.fromRawJson(String str) =>
CompanyListResponseModel.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory CompanyListResponseModel.fromJson(Map<String, dynamic>
json) => CompanyListResponseModel(
nameOfDataDummyOne: json["name_of_data_dummy_one"],
nameOfDataDummyTwo: json["name_of_data_dummy_two"],
nameOfDataDummyThree: json["name_of_data_dummy_three"],
nameOfData: json["name_of_data"] == null ? [] :
List<NameOfDatum>.from(json["name_of_data"]!.map((x) =>
NameOfDatum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"name_of_data_dummy_one": nameOfDataDummyOne,
"name_of_data_dummy_two": nameOfDataDummyTwo,
"name_of_data_dummy_three": nameOfDataDummyThree,
"name_of_data": nameOfData == null ? [] :
List<dynamic>.from(nameOfData!.map((x) => x.toJson())),
};
}
class NameOfDatum {
String? nameOfDataOne;
String? nameOfDataTwo;
String? nameOfDataThree;
String? nameOfDataFour;
String? nameOfDataFive;
String? nameOfDataSix;
String? nameOfDataSeven;
String? nameOfDataEight;
String? nameOfDataNine;
String? nameOfDataTen;
NameOfDatum({
this.nameOfDataOne,
this.nameOfDataTwo,
this.nameOfDataThree,
this.nameOfDataFour,
this.nameOfDataFive,
this.nameOfDataSix,
this.nameOfDataSeven,
this.nameOfDataEight,
this.nameOfDataNine,
this.nameOfDataTen,
});
factory NameOfDatum.fromRawJson(String str) =>
NameOfDatum.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory NameOfDatum.fromJson(Map<String, dynamic> json) =>
NameOfDatum(
nameOfDataOne: json["name_of_data_one"],
nameOfDataTwo: json["name_of_data_two"],
nameOfDataThree: json["name_of_data_three"],
nameOfDataFour: json["name_of_data_four"],
nameOfDataFive: json["name_of_data_five"],
nameOfDataSix: json["name_of_data_six"],
nameOfDataSeven: json["name_of_data_seven"],
nameOfDataEight: json["name_of_data_eight"],
nameOfDataNine: json["name_of_data_nine"],
nameOfDataTen: json["name_of_data_ten"],
);
Map<String, dynamic> toJson() => {
"name_of_data_one": nameOfDataOne,
"name_of_data_two": nameOfDataTwo,
"name_of_data_three": nameOfDataThree,
"name_of_data_four": nameOfDataFour,
"name_of_data_five": nameOfDataFive,
"name_of_data_six": nameOfDataSix,
"name_of_data_seven": nameOfDataSeven,
"name_of_data_eight": nameOfDataEight,
"name_of_data_nine": nameOfDataNine,
"name_of_data_ten": nameOfDataTen,
};
}
and I got error like this "Unhandled Exception: type 'List < dynamic >' is not a subtype of type 'String' in type cast"
My expectation is to create a listview, constrained in parsing data as in the example, by the way I use bloc state management
答案1
得分: 1
Your response is just a single CompanyListResponseModel
but you treat it as if it's a list, which it's not. Change your function to:
Future<Either<String, CompanyListResponseModel>> getCompanyList(
GetListCompanyModel companyListModel) async {
final response = await http.post(
Uri.parse('URL'),
body: thisIsModel.toJson(),
);
if (response.statusCode == 200) {
return CompanyListResponseModel.fromJson(jsonDecode(response.body));
} else {
return const Left('get product error');
}
}
英文:
Your response is just a single CompanyListResponseModel
but you treat it as a if it's a list, which it's not. Change you function to
Future<Either<String, CompanyListResponseModel>> getCompanyList(
GetListCompanyModel companyListModel) async {
final response = await http.post(
Uri.parse('URL'),
body: thisIsModel.toJson(),
);
if (response.statusCode == 200) {
return CompanyListResponseModel.fromJson(jsonDecode(response.body)));
} else {
return const Left('get product error');
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论