Flutter 使用三个模型类的颤振嵌套json解析
我在用flatter解析带有数组的JSON文件时遇到问题。它看起来像这样:Flutter 使用三个模型类的颤振嵌套json解析,flutter,Flutter,我在用flatter解析带有数组的JSON文件时遇到问题。它看起来像这样: { "params": [ { "student_id": "1", "student_name": "name1", "student_dob": "dob1", "student_address": "address1" }, { "student_id": "2", "student_name": "name2",
{
"params": [
{
"student_id": "1",
"student_name": "name1",
"student_dob": "dob1",
"student_address": "address1"
},
{
"student_id": "2",
"student_name": "name2",
"student_dob": "dob2",
"student_address": "address2"
}
],
"error": {
"code": 200,
"message": ""
}
}
您可以使用以下工具从json生成类:
class YourNamedClassObject{
列出参数;
误差;
自动生成({this.params,this.error});
Autogenerated.fromJson(映射json){
if(json['params']!=null){
params=新列表();
json['params'].forEach((v){
params.add(新的params.fromJson(v));
});
}
error=json['error']!=null?新错误。fromJson(json['error']):null;
}
映射到JSON(){
最终地图数据=新地图();
如果(this.params!=null){
data['params']=this.params.map((v)=>v.toJson()).toList();
}
if(this.error!=null){
data['error']=this.error.toJson();
}
返回数据;
}
}
类参数{
弦乐学生;
字符串studentName;
字符串studentDob;
字符串学生地址;
Params(
{this.studentId,this.studentName,this.studentDob,this.studentAddress});
Params.fromJson(映射json){
studentId=json['student_id'];
studentName=json['student_name'];
studentDob=json['student_dob'];
studentAddress=json['student_address'];
}
映射到JSON(){
最终地图数据=新地图();
数据['student_id']=this.studentId;
数据['student_name']=this.studentName;
数据['student_dob']=this.studentDob;
数据['student_address']=this.studentAddress;
返回数据;
}
}
类错误{
int代码;
字符串消息;
错误({this.code,this.message});
Error.fromJson(映射json){
code=json['code'];
message=json['message'];
}
映射到JSON(){
最终地图数据=新地图();
数据['code']=this.code;
数据['message']=此.message;
返回数据;
}
}
您错过了逗号,
在]
结束括号后如何在颤振与实现的列表视图中显示这一点
class YourNamedClassObject {
List<Params> params;
Error error;
Autogenerated({this.params, this.error});
Autogenerated.fromJson(Map<String, dynamic> json) {
if (json['params'] != null) {
params = new List<Params>();
json['params'].forEach((v) {
params.add(new Params.fromJson(v));
});
}
error = json['error'] != null ? new Error.fromJson(json['error']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.params != null) {
data['params'] = this.params.map((v) => v.toJson()).toList();
}
if (this.error != null) {
data['error'] = this.error.toJson();
}
return data;
}
}
class Params {
String studentId;
String studentName;
String studentDob;
String studentAddress;
Params(
{this.studentId, this.studentName, this.studentDob, this.studentAddress});
Params.fromJson(Map<String, dynamic> json) {
studentId = json['student_id'];
studentName = json['student_name'];
studentDob = json['student_dob'];
studentAddress = json['student_address'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['student_id'] = this.studentId;
data['student_name'] = this.studentName;
data['student_dob'] = this.studentDob;
data['student_address'] = this.studentAddress;
return data;
}
}
class Error {
int code;
String message;
Error({this.code, this.message});
Error.fromJson(Map<String, dynamic> json) {
code = json['code'];
message = json['message'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['code'] = this.code;
data['message'] = this.message;
return data;
}
}