簡體   English   中英

Flutter:如何將列表轉換為JSON

[英]Flutter: How to convert a List to JSON

我正在嘗試將列表轉換為Json並將此json發送到DB。

我的清單如下

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});
}

我的列表包含String和boolean,我已經看過了這個示例,但不知道如何在該map函數中映射string和bool。 誰能幫我這個?

幾個選項將有助於從JSON編碼和解碼: json_serializable軟件包是一種為您生成樣板代碼序列化/反序列化代碼的好方法。 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}");

是我記得的最短的方法。

將 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> 轉換清單 <T> 撲入json 如何將數據從json轉換為List<Object> 在顫振中 如何將 JSON 字符串轉換為列表<string>在 flutter 中?</string> 在flutter中將forEach List轉換成json列表 如何將此json轉換為Flutter對象 如何將 Json 轉換為顫振形式 如何在 flutter 中將 object 轉換為 json? 將 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> 將檢索到的 json 轉換為 flutter 中的自定義對象列表
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM