json响应如下:
{
"name": "A name",
"international": {
"name": {
"en": "A name",
"fr": "Un nom"
}
}
}
我试着用下面的代码来反序列化它:
import 'package:json_annotation/json_annotation.dart';
import 'dart:convert';
part 'model.g.dart';
void main() {
var json = '''{
"name": "A name",
"international": {
"name": {
"en": "A name",
"fr": "Un nom"
}
}
}''';
var model = Model.fromJson(jsonDecode(json));
}
@JsonSerializable()
class Model {
Model({
required this.name,
required this.internationalNames,
});
factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
final String name;
@JsonKey(readValue: _readInternational, name: "name")
final Map<String, String> internationalNames;
static Object? _readInternational(Map<dynamic, dynamic> json, String key) =>
json['international'][key];
}
我得到一个错误时运行build_runner:
多个字段具有name" name" JSON键
,尽管internationalNames嵌套在国际对象中,所以没有重复的键。我该怎么做才能把它反序列化呢?
我通过创建一个InternationalModel类来让它工作:
import 'package:json_annotation/json_annotation.dart';
import 'dart:convert';
part 'temp.g.dart';
void main() {
var json = '''{
"name": "A name",
"international": {
"name": {
"en": "A name",
"fr": "Un nom"
}
}
}''';
var model = Model.fromJson(jsonDecode(json));
}
@JsonSerializable()
class Model {
Model({
required this.name,
required this.international,
});
factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
final String name;
final InternationalModel international;
}
@JsonSerializable()
class InternationalModel {
InternationalModel({
required this.names,
});
factory InternationalModel.fromJson(Map<String, dynamic> json) =>
_$InternationalModelFromJson(json);
@JsonKey(name: 'name')
final Map<String, String> names;
}
感谢Nagual为我指出了这个解决方案。
我可以使用下面的代码解码它。
import 'dart:convert';
void main() {
var json = '''{
"name": "A name",
"international": {
"name": {
"en": "A name",
"fr": "Un nom"
}
}
}''';
Model model = Model.fromJson(jsonDecode(json));
print(model.international!.name!.en);
}
class Model {
String? name;
International? international;
Model({this.name, this.international});
Model.fromJson(Map<String, dynamic> json) {
name = json['name'];
international = json['international'] != null
? new International.fromJson(json['international'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
if (this.international != null) {
data['international'] = this.international!.toJson();
}
return data;
}
}
class International {
Name? name;
International({this.name});
International.fromJson(Map<String, dynamic> json) {
name = json['name'] != null ? new Name.fromJson(json['name']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.name != null) {
data['name'] = this.name!.toJson();
}
return data;
}
}
class Name {
String? en;
String? fr;
Name({this.en, this.fr});
Name.fromJson(Map<String, dynamic> json) {
en = json['en'];
fr = json['fr'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['en'] = this.en;
data['fr'] = this.fr;
return data;
}
}