【发布时间】:2019-10-04 13:43:22
【问题描述】:
我有一个甜甜圈类,它有自己的属性,我创建了一个甜甜圈对象列表,我想在 JSON 中对其进行编码和解码。我怎样才能做到这一点?我已经实现了对对象本身的解析,但是在尝试对这些对象的列表执行相同操作时遇到了麻烦。
import 'package:flutter/material.dart';
class Doughnut {
String name;
String filling;
List<String> toppings;
double price;
var days;
Doughnut(this.name, this.filling, this.toppings, this.price,this.days);
Doughnut.fromJson(Map<String, dynamic> json) {
name = json['name'];
filling = json['filling'];
var toppingsFromJson = json['toppings'];
toppings = new List<String>.from(toppingsFromJson);
price = json['price'];
var daysFromJson=json["days"];
days=new Map<String,bool>.from(daysFromJson);
}
Map<String, dynamic> toJson() =>
{"name": name, "filling": filling, 'toppings': toppings, "price": price,"days":days};
}
【问题讨论】: