[英]Flutter: How to convert a List to JSON
I am trying to convert a list to Json and sent this json to DB. 我正在尝试将列表转换为Json并将此json发送到DB。
My list is as following 我的清单如下
List<DeviceInfo> deviceInfoList = [];
class DeviceInfo {
final String platform;
final String deviceModel;
final bool isPhysicalDevice;
final String deviceId;
final String imei;
final String meid;
final String platformVersion;
final String projectVersion;
final String projectCode;
final String projectAppID;
final String projectName;
DeviceInfo(
{this.platform,
this.platformVersion,
this.deviceModel,
this.isPhysicalDevice,
this.deviceId,
this.imei,
this.meid,
this.projectVersion,
this.projectCode,
this.projectAppID,
this.projectName});
}
My list contain String and boolean, I had go through this example don't know how to Map string and bool in that map function. 我的列表包含String和boolean,我已经看过了这个示例,但不知道如何在该map函数中映射string和bool。 Can anyone help me with this? 谁能帮我这个?
Couple of options that will help encoding and decoding from JSON: the json_serializable package is a great way to have the boilerplate serialize/deserialize code generated for you. 几个选项将有助于从JSON编码和解码: json_serializable软件包是一种为您生成样板代码序列化/反序列化代码的好方法。 There's examples of how to use this (and built_value , which is powerful, but more complicated to use) in the Flutter samples repo . 在Flutter样本回购中有示例说明了如何使用此示例(以及built_value ,功能强大,但使用起来更复杂)。
Map<String,dynamic> toJson(){
return {
"name": this.name,
"number": this.number,
"surname": this.surname,
};
}
static List encondeToJson(List<DeviceInfo>list){
List jsonList = List();
list.map((item)=>
jsonList.add(item.toJson())
).toList();
return jsonList;
}
List jsonList = Device.encondeToJson(deviceInfoList);
print("jsonList: ${jsonList}");
Is the most short way that I remember. 是我记得的最短的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.