flutter, json转化、toJson、fromJson、enum枚举类型toJson、fromJson

 

顾名思义,flutter中的json转化其实涉及到很多方面,如 print打印,toString方法显示、toJson方法显示、序列化和反序列化等等。

其实总结一句话,就是dart类型的序列化反序列化,道理类似于java中的Serilazable序列化和反序列化一样的。

直接上代码:

/// 购买时选择的套餐
  static savePurchaseWhichItem(PurchaseItem item) async {
    if (item != null) {
      SharedPreferences preferences = await SharedPreferences.getInstance();
      await preferences.setString(
          purchase_which_item, jsonEncode(item));
      print("sp savePurchaseWhichItem setString = ${jsonEncode(item)}");
    }
  }

例如,我要保存PurchaseItem这个实体类信息,转化为String类型进行保存,用了jsonEncode方法进行转化,那么转化时需要注意几个问题:

1.PurchaseItem类必须要实现toJson方法,而且其中的其他实体类型(IAPItem和ProductType)也必须要实现toJson方法

2.PurchaseItem实体类和IAPItem实现toJson方法,就是逐个字段进行设置,例如:

final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;

但是enum枚举类型实现序列化和反序列化就麻烦点;

toJson方法:

if (this.productType != null) {
      data['productType'] = json.encode(productType.toString());
    }

fromJson方法:

productType = _getProductType(jsonDecode(json['productType'])),
/// 枚举enum类型比较
  static ProductType _getProductType(String productTypeStr) {
    for (ProductType type in ProductType.values) {
      if (type.toString() == productTypeStr) {
        return type;
      }
    }

    return null;
  }

3.PurchaseItem类必须要实现fromJson方法,而且其中的其他实体类型(IAPItem和ProductType)也必须要实现fromJson方法。

 

其实刚才这里是利用enum枚举类型的name属性来完成的序列化和反序列化,

还可以有另外一种方式,利用enum枚举类型的index属性类完成:

toJson方法:

if (this.productType != null) {
      data['productType'] = json.encode(productType.index);
    }

fromJson方法:

productType = _getProductType(jsonDecode(json['productType'])),
/// 枚举enum类型比较
  static ProductType _getProductType(int index) {
    return ProductType.values[index];
  }

 

 

最后上代码:

PurchaseItem类代码如下,其中包括IAPItem实体类和ProductType枚举类型,所以在序列化和反序列化时需要注意toJson和fromJson方法中item和productType的转化问题;(其中IAPItem类的toJson和fromJson方法就省略了,太长了,就是逐个字段逐个字段的设置和反设置)

class PurchaseItem {
  final String title;
  final String price;
  final String symbol;
  final String desc;
  final String monthPrice; // 每月的价格
  final IAPItem item;
  final ProductType productType;
  final String source;

  PurchaseItem({
    this.title,
    this.price,
    this.symbol,
    this.desc,
    this.monthPrice,
    this.item,
    this.productType,
    this.source,
  });

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['price'] = this.price;
    data['symbol'] = this.symbol;
    data['desc'] = this.desc;
    data['monthPrice'] = this.monthPrice;
    if (this.item != null) {
      data['item'] = this.item.toJson();
    }
    if (this.productType != null) {
      data['productType'] = json.encode(productType.toString());
    }
    data['source'] = this.source;
    return data;
  }

  PurchaseItem.fromJSON(Map<String, dynamic> json)
      : title = json['title'] as String,
        source = json['source'] as String,
        price = json['price'] as String,
        symbol = json['symbol'] as String,
        item = IAPItem.fromJSON(json['item']),
        desc = json['desc'] as String,
        productType = _getProductType(jsonDecode(json['productType'])),
        monthPrice = json['monthPrice'] as String;

/// 枚举enum类型比较
  static ProductType _getProductType(String productTypeStr) {
    for (ProductType type in ProductType.values) {
      if (type.toString() == productTypeStr) {
        return type;
      }
    }

    return null;
  }
  }
enum ProductType {
  month,
  threeMonth,
  sixMonth,
  year,
  lifeTime,
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值