我有一个具有其自身属性的甜甜圈类,我创建了一个甜甜圈对象列表,并且希望使用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};
}
答案 0 :(得分:0)
您必须解码数组:
var toppingsFromJson = jsonDecode(json['toppings']);