顾名思义,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,
}
本文详细解析了Flutter中JSON的序列化与反序列化过程,重点介绍了实体类PurchaseItem的转换方法,包括如何实现toJson和fromJson方法,以及处理枚举类型ProductType的序列化与反序列化的技巧。
647

被折叠的 条评论
为什么被折叠?



