如何在飞镖中创建 json('_CompactLinkedHashSet 的异常实例)
How to create a json in dart (Exception Instance of '_CompactLinkedHashSet)
我正在尝试将一些对象转换为 json 以将其发送到数据库,这是我现在的代码:
Map<String, dynamic> bag = {};
bag = {
'colors' : [
for(int i = 0; i < product.colors.length; i ++){
'id': product.colors[i].id,
'desc': product.colors[i].desc,
'sizes': [
for(int j=0; j < product.colors[i].sizes.length; j ++){
product.colors[i].sizes[j].toJson()
}
]
}]
};
print(json.encode(bag));
但我得到的结果是这样的:
Converting object to an encodable object failed: Instance of '_CompactLinkedHashSet<Map<String, dynamic>>'
我该如何解决?
产品class:
final String id;
final String desc;
final List<Colors> colors;
颜色:
final String id;
final String desc;
final List<Prices> prices;
价格:
final String id;
final String desc;
final List<Types> types;
类型:
final String id;
final String desc;
final List<Sizes> sizes;
尺码:
final String id;
final int stock;
final String desc;
final bool filled;
int quantity;
final double price;
我想要这样的json:
colors:
id : 12
desc : black
sizes: [
id : 0202202
stock : 10
desc: Full Size
filled: true
quantity : 2
price : 200
]
您创建 JSON 的方式太复杂了。尝试在每个 类 上添加一个 toJson()
方法。如果 Dart JSON 编码器命中一个与 JSON.
不兼容的对象,它会自动调用此方法
这是一个例子:
import 'dart:convert';
class Product {
final String id;
final String desc;
final List<Colors> colors;
Product({
required this.id,
required this.desc,
required this.colors,
});
Map<String, Object> toJson() => {
'id': id,
'desc': desc,
'colors': colors,
};
}
class Colors {
final int id;
final String desc;
final List<Prices> prices;
Colors({
required this.id,
required this.desc,
required this.prices,
});
Map<String, Object> toJson() => {
'id': id,
'desc': desc,
'prices': prices,
};
}
class Prices {
final String id;
final String desc;
final List<Types> types;
Prices({
required this.id,
required this.desc,
required this.types,
});
Map<String, Object> toJson() => {
'id': id,
'desc': desc,
'types': types,
};
}
class Types {
final String id;
final String desc;
final List<Sizes> sizes;
Types({
required this.id,
required this.desc,
required this.sizes,
});
Map<String, Object> toJson() => {
'id': id,
'desc': desc,
'sizes': sizes,
};
}
class Sizes {
final String id;
final int stock;
final String desc;
final bool filled;
int quantity;
final double price;
Sizes({
required this.id,
required this.stock,
required this.desc,
required this.filled,
required this.quantity,
required this.price,
});
Map<String, Object> toJson() => {
'id': id,
'stock': stock,
'desc': desc,
'filled': filled,
'quantity': quantity,
'price': price,
};
}
void main() {
final product = Product(id: '1', desc: 'Some text', colors: [
Colors(id: 1, desc: '', prices: [
Prices(id: '1', desc: '', types: [
Types(id: '1', desc: '', sizes: [
Sizes(
id: '1',
stock: 5,
desc: '',
filled: true,
quantity: 2,
price: 1.0)
])
])
]),
Colors(id: 2, desc: '', prices: [
Prices(id: '1', desc: '', types: [
Types(id: '1', desc: '', sizes: [
Sizes(
id: '5',
stock: 5,
desc: '',
filled: true,
quantity: 2,
price: 1.0),
Sizes(
id: '50',
stock: 5,
desc: '',
filled: true,
quantity: 2,
price: 1.0),
])
])
])
]);
final bag = {
'colors': [
for (final colors in product.colors)
{
'id': colors.id,
'desc': colors.desc,
'sizes': [
for (final prices in colors.prices)
for (final types in prices.types) ...types.sizes
]
}
]
};
print(const JsonEncoder.withIndent(' ').convert(bag));
}
输出:
{
"colors": [
{
"id": 1,
"desc": "",
"sizes": [
{
"id": "1",
"stock": 5,
"desc": "",
"filled": true,
"quantity": 2,
"price": 1.0
}
]
},
{
"id": 2,
"desc": "",
"sizes": [
{
"id": "5",
"stock": 5,
"desc": "",
"filled": true,
"quantity": 2,
"price": 1.0
},
{
"id": "50",
"stock": 5,
"desc": "",
"filled": true,
"quantity": 2,
"price": 1.0
}
]
}
]
}
这里的一个技巧是 Dart 将在另一个 toJson()
调用返回的结构中调用对象。所以例如Product.toJson
returns 包含颜色列表的 Map<String, Object>
。然后 Dart 对这些颜色中的每一种调用 toJson()
,依此类推。
这个应该对你有帮助,有地图的时候最好转换成json,所以你需要把class和它的子class转换成地图,并且在最后转换成 json.
会很简单
要映射的产品class:
class Product {
final String id;
final String desc;
final List<Colors> colors;
Product(this.id, this.desc, this.colors);
Map toMap() {
return {
'id': this.id,
'desc': this.desc,
'colors': this.colors.map((color) => color.toMap()).toList(),
};
}
}
要映射的颜色 class:
class Colors {
final String id;
final String desc;
final List<Sizes> sizes;
Colors(this.id, this.desc, this.sizes);
Map toMap() {
return {
'id': this.id,
'desc': this.desc,
'sizes': this.sizes.map((size) => size.toMap()).toList(),
};
}
}
要映射的尺寸 class:
class Sizes {
final String id;
final int stock;
final String desc;
final bool filled;
int quantity;
final double price;
Sizes(this.id, this.stock, this.desc, this.filled, this.quantity, this.price);
Map toMap() {
return {
'id': this.id,
'stock': this.stock,
'desc': this.desc,
'filled': this.filled,
'quantity': this.quantity,
'price': this.price,
};
}
}
结果:
Product product = Product(...);
Map map = product.toMap();
String json = jsonEncode(map);
我正在尝试将一些对象转换为 json 以将其发送到数据库,这是我现在的代码:
Map<String, dynamic> bag = {};
bag = {
'colors' : [
for(int i = 0; i < product.colors.length; i ++){
'id': product.colors[i].id,
'desc': product.colors[i].desc,
'sizes': [
for(int j=0; j < product.colors[i].sizes.length; j ++){
product.colors[i].sizes[j].toJson()
}
]
}]
};
print(json.encode(bag));
但我得到的结果是这样的:
Converting object to an encodable object failed: Instance of '_CompactLinkedHashSet<Map<String, dynamic>>'
我该如何解决?
产品class:
final String id;
final String desc;
final List<Colors> colors;
颜色:
final String id;
final String desc;
final List<Prices> prices;
价格:
final String id;
final String desc;
final List<Types> types;
类型:
final String id;
final String desc;
final List<Sizes> sizes;
尺码:
final String id;
final int stock;
final String desc;
final bool filled;
int quantity;
final double price;
我想要这样的json:
colors:
id : 12
desc : black
sizes: [
id : 0202202
stock : 10
desc: Full Size
filled: true
quantity : 2
price : 200
]
您创建 JSON 的方式太复杂了。尝试在每个 类 上添加一个 toJson()
方法。如果 Dart JSON 编码器命中一个与 JSON.
这是一个例子:
import 'dart:convert';
class Product {
final String id;
final String desc;
final List<Colors> colors;
Product({
required this.id,
required this.desc,
required this.colors,
});
Map<String, Object> toJson() => {
'id': id,
'desc': desc,
'colors': colors,
};
}
class Colors {
final int id;
final String desc;
final List<Prices> prices;
Colors({
required this.id,
required this.desc,
required this.prices,
});
Map<String, Object> toJson() => {
'id': id,
'desc': desc,
'prices': prices,
};
}
class Prices {
final String id;
final String desc;
final List<Types> types;
Prices({
required this.id,
required this.desc,
required this.types,
});
Map<String, Object> toJson() => {
'id': id,
'desc': desc,
'types': types,
};
}
class Types {
final String id;
final String desc;
final List<Sizes> sizes;
Types({
required this.id,
required this.desc,
required this.sizes,
});
Map<String, Object> toJson() => {
'id': id,
'desc': desc,
'sizes': sizes,
};
}
class Sizes {
final String id;
final int stock;
final String desc;
final bool filled;
int quantity;
final double price;
Sizes({
required this.id,
required this.stock,
required this.desc,
required this.filled,
required this.quantity,
required this.price,
});
Map<String, Object> toJson() => {
'id': id,
'stock': stock,
'desc': desc,
'filled': filled,
'quantity': quantity,
'price': price,
};
}
void main() {
final product = Product(id: '1', desc: 'Some text', colors: [
Colors(id: 1, desc: '', prices: [
Prices(id: '1', desc: '', types: [
Types(id: '1', desc: '', sizes: [
Sizes(
id: '1',
stock: 5,
desc: '',
filled: true,
quantity: 2,
price: 1.0)
])
])
]),
Colors(id: 2, desc: '', prices: [
Prices(id: '1', desc: '', types: [
Types(id: '1', desc: '', sizes: [
Sizes(
id: '5',
stock: 5,
desc: '',
filled: true,
quantity: 2,
price: 1.0),
Sizes(
id: '50',
stock: 5,
desc: '',
filled: true,
quantity: 2,
price: 1.0),
])
])
])
]);
final bag = {
'colors': [
for (final colors in product.colors)
{
'id': colors.id,
'desc': colors.desc,
'sizes': [
for (final prices in colors.prices)
for (final types in prices.types) ...types.sizes
]
}
]
};
print(const JsonEncoder.withIndent(' ').convert(bag));
}
输出:
{
"colors": [
{
"id": 1,
"desc": "",
"sizes": [
{
"id": "1",
"stock": 5,
"desc": "",
"filled": true,
"quantity": 2,
"price": 1.0
}
]
},
{
"id": 2,
"desc": "",
"sizes": [
{
"id": "5",
"stock": 5,
"desc": "",
"filled": true,
"quantity": 2,
"price": 1.0
},
{
"id": "50",
"stock": 5,
"desc": "",
"filled": true,
"quantity": 2,
"price": 1.0
}
]
}
]
}
这里的一个技巧是 Dart 将在另一个 toJson()
调用返回的结构中调用对象。所以例如Product.toJson
returns 包含颜色列表的 Map<String, Object>
。然后 Dart 对这些颜色中的每一种调用 toJson()
,依此类推。
这个应该对你有帮助,有地图的时候最好转换成json,所以你需要把class和它的子class转换成地图,并且在最后转换成 json.
会很简单要映射的产品class:
class Product {
final String id;
final String desc;
final List<Colors> colors;
Product(this.id, this.desc, this.colors);
Map toMap() {
return {
'id': this.id,
'desc': this.desc,
'colors': this.colors.map((color) => color.toMap()).toList(),
};
}
}
要映射的颜色 class:
class Colors {
final String id;
final String desc;
final List<Sizes> sizes;
Colors(this.id, this.desc, this.sizes);
Map toMap() {
return {
'id': this.id,
'desc': this.desc,
'sizes': this.sizes.map((size) => size.toMap()).toList(),
};
}
}
要映射的尺寸 class:
class Sizes {
final String id;
final int stock;
final String desc;
final bool filled;
int quantity;
final double price;
Sizes(this.id, this.stock, this.desc, this.filled, this.quantity, this.price);
Map toMap() {
return {
'id': this.id,
'stock': this.stock,
'desc': this.desc,
'filled': this.filled,
'quantity': this.quantity,
'price': this.price,
};
}
}
结果:
Product product = Product(...);
Map map = product.toMap();
String json = jsonEncode(map);