我有一个邮递员 API 调用。但我不知道如何在邮递员 API 的 API 调用中从应用程序 JSON 值中提取数据。有人可以向我解释一下如何在 dart 数据模型中提取它吗?
{
"deviceType": "andriod",
"deviceId": "C6179909526098",
"deviceName": "Samsung-MT200",
"deviceOSVersion": "2.3.6",
"deviceIPAddress": "11.433.445.66",
"lat": 9.9312,
"long": 76.2673,
"buyer_gcmid": "",
"buyer_pemid": "",
"app": {
"version": "1.20.5",
"installTimeStamp": "2022-02-10T12:33:30.696Z",
"uninstallTimeStamp": "2022-02-10T12:33:30.696Z",
"downloadTimeStamp": "2022-02-10T12:33:30.696Z"
}
}
这是数据模型。
class SplashPost {
String? deviceType;
String? deviceId;
String? deviceName;
String? deviceOSVerion;
String? deviceIPAddress;
String? lat;
String? long;
String? buyer_gcmid;
String? buyer_pemid;
String? version;
List<dynamic>? app;
SplashPost({
required this.deviceType,
required this.deviceIPAddress,
required this.deviceId,
required this.deviceName,
required this.deviceOSVerion,
required this.app,
required this.buyer_gcmid,
required this.buyer_pemid,
required this.lat,
required this.long,
required this.version,
});
factory SplashPost.fromJson(Map<String, dynamic> json) => SplashPost(
deviceIPAddress: json["deviceIPAddress"],
deviceType: json["deviceType"],
deviceId: json["deviceId"],
deviceName: json["deviceName"],
deviceOSVerion: json["deviceOSVersion"],
lat: json["lat"],
long: json["long"],
version: json["version"],
buyer_gcmid: json["buyer_gcmid"],
buyer_pemid: json["buyer_pemid"],
app: List<dynamic>.from(json["app"]).map((x) => x),
);
}
Map<String, dynamic> toJson() => {
"version": version,
"installTimeStamp": installTimeStamp,
"uninstallTimeStamp": uninstallTimeStamp,
"downloadTimeStamp": downloadTimeStamp,
};
这是数据模型文件,我不知道如何从“app”JSON 值中提取数据。 它让我犯了
的错误参数类型“Iterable”无法分配给参数类型“List”?在应用程序价值列表中。
和
未定义名称“版本”
有人可以解释并解决我的问题吗。
我个人喜欢使用网站 https://app.quicktype.io/ 从响应生成一个类。它有一些设置可以根据您的喜好进行调整,如果设置不能涵盖它,您当然可以手动进行调整。这是该网站的示例输出,使用您的回复。
import 'dart:convert';
class SplashPost {
String deviceType;
String deviceId;
String deviceName;
String deviceOsVersion;
String deviceIpAddress;
double lat;
double long;
String buyerGcmid;
String buyerPemid;
App app;
SplashPost({
required this.deviceType,
required this.deviceId,
required this.deviceName,
required this.deviceOsVersion,
required this.deviceIpAddress,
required this.lat,
required this.long,
required this.buyerGcmid,
required this.buyerPemid,
required this.app,
});
factory SplashPost.fromJson(String str) => SplashPost.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory SplashPost.fromMap(Map<String, dynamic> json) => SplashPost(
deviceType: json["deviceType"],
deviceId: json["deviceId"],
deviceName: json["deviceName"],
deviceOsVersion: json["deviceOSVersion"],
deviceIpAddress: json["deviceIPAddress"],
lat: json["lat"]?.toDouble(),
long: json["long"]?.toDouble(),
buyerGcmid: json["buyer_gcmid"],
buyerPemid: json["buyer_pemid"],
app: App.fromMap(json["app"]),
);
Map<String, dynamic> toMap() => {
"deviceType": deviceType,
"deviceId": deviceId,
"deviceName": deviceName,
"deviceOSVersion": deviceOsVersion,
"deviceIPAddress": deviceIpAddress,
"lat": lat,
"long": long,
"buyer_gcmid": buyerGcmid,
"buyer_pemid": buyerPemid,
"app": app.toMap(),
};
}
class App {
String version;
DateTime installTimeStamp;
DateTime uninstallTimeStamp;
DateTime downloadTimeStamp;
App({
required this.version,
required this.installTimeStamp,
required this.uninstallTimeStamp,
required this.downloadTimeStamp,
});
factory App.fromJson(String str) => App.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory App.fromMap(Map<String, dynamic> json) => App(
version: json["version"],
installTimeStamp: DateTime.parse(json["installTimeStamp"]),
uninstallTimeStamp: DateTime.parse(json["uninstallTimeStamp"]),
downloadTimeStamp: DateTime.parse(json["downloadTimeStamp"]),
);
Map<String, dynamic> toMap() => {
"version": version,
"installTimeStamp": installTimeStamp.toIso8601String(),
"uninstallTimeStamp": uninstallTimeStamp.toIso8601String(),
"downloadTimeStamp": downloadTimeStamp.toIso8601String(),
};
}
您的
json["app"]
属于 Map<String, dynamic> json
类型,这就是为什么 List<dynamic>.from(json["app"])
不起作用。
您需要将
app
的类型更改为:
Map<String, dynamic> app;
并像这样解析它
factory SplashPost.fromJson(Map<String, dynamic> json) => SplashPost(
// ... rest of code
app: json['app'] ?? {}
)
或者您可以创建一个新课程
SplashPostApp
并实施
factory SplashPostApp.fromJson(Map<String, dynamic> json) ...
然后在您的
SplashPost
类中将 app
的类型更改为 SplashPostApp
并将其解析为:
factory SplashPost.fromJson(Map<String, dynamic> json) => SplashPost(
...
app: SplashPostApp.fromJson(json['app']?? {})