简体   繁体   English

Flutter:如何将列表转换为JSON

[英]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. 是我记得的最短的方法。

将 Json 数组转换为列表<object>在 Flutter<div id="text_translate"><p> 我是 Flutter 和 Dart 的新手,我一直在努力保存共享首选项中的值以在我的应用程序重新启动时使用。 我已经成功存储了变量,但我还需要存储 object 列表。 我知道由于共享首选项只接受字符串列表,我需要将我的 object 列表转换为 JSON 数组,当应用程序重新启动时,检索此列表并将其再次转换为 object 列表。</p><p> 我能够将 object 列表编码为 JSON 并且得到如下信息:</p><pre> [{name: Rent, amount: 250}, {name: Insurance, amount: 105}]</pre><p> 我通过以下 function 实现了这一点:</p><pre> void SaveLists(key, value) async { //where value is a List&lt;Object&gt; final prefs = await SharedPreferences.getInstance(); List&lt;dynamic&gt; json_list = (value.map((i) =&gt; i.toJson())).toList(); prefs.setStringList(key, json_list); //this line causes the error print('$json_list'); }</pre><p> List json_list 包含如上所示的 JSON 数组。 但是,当我尝试使用prefs.setStringList(key, json_list); 我收到以下错误:</p><pre> Unhandled Exception: type 'List&lt;dynamic&gt;' is not a subtype of type 'List&lt;String&gt;'</pre><p> 现在,假设我以某种方式成功地将其存储在共享首选项中,如何在使用prefs.getString('key')调用时将 JSON 数组转换回 List ?</p><p> 如果您需要查看 class,这里是:</p><pre> import 'dart:convert'; class Random_expenses { String name; double amount; Random_expenses({this.name, this.amount}); Random_expenses.fromJson(Map&lt;String, dynamic&gt; json): this.name = json['name'], this.amount = json['amount']; Map&lt;String, dynamic&gt; toJson() =&gt; {'name': this.name, 'amount': this.amount}; }</pre></div></object> - Convert Json Array to List<Object> in Flutter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 json 数据转换为列表<widgets>在 flutter 中?</widgets> - How to convert a json data into a List<widgets> in flutter? 转换清单 <T> 扑入json - Convert List<T> into json in flutter 如何将数据从json转换为List<Object> 在颤振中 - How to convert data from json to List<Object> in Flutter 如何将 JSON 字符串转换为列表<string>在 flutter 中?</string> - How can i convert a JSON String to List<String> in flutter? 在flutter中将forEach List转换成json列表 - Convert forEach List into json list in flutter 如何将此json转换为Flutter对象 - How to convert this json to Flutter Object 如何将 Json 转换为颤振形式 - How to convert Json to form in flutter 如何在 flutter 中将 object 转换为 json? - How to convert object to json in flutter? 将 Json 数组转换为列表<object>在 Flutter<div id="text_translate"><p> 我是 Flutter 和 Dart 的新手,我一直在努力保存共享首选项中的值以在我的应用程序重新启动时使用。 我已经成功存储了变量,但我还需要存储 object 列表。 我知道由于共享首选项只接受字符串列表,我需要将我的 object 列表转换为 JSON 数组,当应用程序重新启动时,检索此列表并将其再次转换为 object 列表。</p><p> 我能够将 object 列表编码为 JSON 并且得到如下信息:</p><pre> [{name: Rent, amount: 250}, {name: Insurance, amount: 105}]</pre><p> 我通过以下 function 实现了这一点:</p><pre> void SaveLists(key, value) async { //where value is a List&lt;Object&gt; final prefs = await SharedPreferences.getInstance(); List&lt;dynamic&gt; json_list = (value.map((i) =&gt; i.toJson())).toList(); prefs.setStringList(key, json_list); //this line causes the error print('$json_list'); }</pre><p> List json_list 包含如上所示的 JSON 数组。 但是,当我尝试使用prefs.setStringList(key, json_list); 我收到以下错误:</p><pre> Unhandled Exception: type 'List&lt;dynamic&gt;' is not a subtype of type 'List&lt;String&gt;'</pre><p> 现在,假设我以某种方式成功地将其存储在共享首选项中,如何在使用prefs.getString('key')调用时将 JSON 数组转换回 List ?</p><p> 如果您需要查看 class,这里是:</p><pre> import 'dart:convert'; class Random_expenses { String name; double amount; Random_expenses({this.name, this.amount}); Random_expenses.fromJson(Map&lt;String, dynamic&gt; json): this.name = json['name'], this.amount = json['amount']; Map&lt;String, dynamic&gt; toJson() =&gt; {'name': this.name, 'amount': this.amount}; }</pre></div></object> - Convert Json Array to List<Object> in Flutter 将检索到的 json 转换为 flutter 中的自定义对象列表 - convert retrieved json to a list of custom objects in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM