转换列表< T>扑入json [英] Convert List<T> into json in flutter

查看:61
本文介绍了转换列表< T>扑入json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当选择特定选项的属性时,我有一个选项列表,然后将属性对象保存在列表中选项的所选位置。
现在我有一个带有其选定属性对象的选项列表。我的目的是将选项列表转换为JSON对象,但是当属性不为null时。在这种情况下,如果人们没有选择选项的属性,则Attribute对象可以为null。

I have a list of option when the attribute is selected of a particular option then I save attribute object at the selected position of option in the list. now I have an option list with their selected attribute object. My aim is to convert the options list into JSON object but when the attribute is not null. The Attribute object can be null in that case when a person has not chosen an attribute of an option.

class OptionAttribute{
 String _grouprowid;
 String _groupname;
 Attribute _selectedAttrObject

   Map<String, dynamic> toJson() => {
    'attribute': _selectedAttrObject,
  };
}

class Attribute{

  String _attributerowid;
  String _grouprowid;
  String _attributename;
  String _weight;

   Map<String, dynamic> toJsonAttr() => {
    'attrid': _attributerowid,
    'groupid': _grouprowid,
    'attrname': _attributename
  };

}

我想将以下列表转换为JSON对象

I want to convert below list into JSON object when the list does not have any null attribute.

List<OptionAttribute> opAtrrList=new List<OptionAttribute>();


推荐答案

您需要单独转换每个项目

You need to convert each item individually

var json = jsonEncode(opAttrList.map((e) => e.toJson()).toList());

或将传递给Encodable 函数

var json = jsonEncode(opAttrList, toEncodable: (e) => e.toJsonAttr());

这篇关于转换列表&lt; T&gt;扑入json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆