【发布时间】:2021-01-24 15:23:53
【问题描述】:
我需要将 JSON 解析为对象并在我的应用程序中使用它,但我需要使用 dio 库来执行此操作,但我是新手,谁能帮助我如何使用它将 JSON 解析为对象,我的请求也需要一个令牌,我的对象将像这样锁定:
import 'dart:convert';
Users usersFromJson(String str) => Users.fromJson(json.decode(str));
String usersToJson(Users data) => json.encode(data.toJson());
class Users {
Users({
this.data,
});
List<Datum> data;
factory Users.fromJson(Map<String, dynamic> json) => Users(
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
Datum({
this.id,
this.name,
this.email,
this.phone,
this.status,
this.images,
});
int id;
String name;
String email;
String phone;
String status;
List<Image> images;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
id: json["id"],
name: json["name"],
email: json["email"],
phone: json["phone"],
status: json["status"],
images: List<Image>.from(json["images"].map((x) => Image.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"email": email,
"phone": phone,
"status": status,
"images": List<dynamic>.from(images.map((x) => x.toJson())),
};
}
class Image {
Image({
this.id,
this.url,
this.isProfileImage,
});
int id;
String url;
int isProfileImage;
factory Image.fromJson(Map<String, dynamic> json) => Image(
id: json["id"],
url: json["url"],
isProfileImage: json["is_profile_image"],
);
Map<String, dynamic> toJson() => {
"id": id,
"url": url,
"is_profile_image": isProfileImage,
};
}
谁能帮我一步一步地使用provider和dio来做这件事!
【问题讨论】: