【发布时间】:2019-04-22 08:33:03
【问题描述】:
我使用json_serializable 库和firebase 作为数据库。我需要将我的数据类序列化和反序列化到 JSON。但是firebase json格式是这样的
{
'some_id_here': {
'title': 'some_title',
'content': 'some_content',
'timestamp': 'some_timestamp'
}, ...
}
这是我的课
import 'package:json_annotation/json_annotation.dart';
part 'update.g.dart';
@JsonSerializable()
class Update {
final String id, title, content, timestamp;
Update({this.id, this.title, this.content, this.timestamp});
factory Update.fromJson(Map<String, dynamic> json) => _$UpdateFromJson(json);
Map<String, dynamic> toJson() {
return _$UpdateToJson(this);
}
}
那么如何自定义 toJson() 函数以便我可以反序列化
Update('ID', 'TITLE', 'CONTENT', 'TIMESTAMP');
到
{
'ID': {
'title': 'TITLE',
'content': 'CONTENT',
'timestamp': 'TIMESTAMP'
}
}
【问题讨论】: