嵌套对象的JSON序列化飞镖

发布于 2025-02-13 04:59:33 字数 3533 浏览 0 评论 0原文

我正在尝试使用JSON_Serializable库为我的嵌套对象生成FromJSON和TOJSON功能。但是,当我尝试使用生成的函数时,我遇到了一个错误的嵌套对象。

我想我了解如何写托伊森和源头功能。但是我想弄清楚如何与JSON_Serializable库一起使用并使用生成的代码。

与模型的类:

import 'package:json_annotation/json_annotation.dart';
part 'models.g.dart';

@JsonSerializable()
class OfficesList {
  List<Office> offices;
  OfficesList({required this.offices});

  factory OfficesList.fromJson(Map<String, dynamic> json) => _$OfficesListFromJson(json);

  Map<String, dynamic> toJson() => _$OfficesListToJson(this);
}

@JsonSerializable()
class Office {
  final String name;
  final String address;
  final String image;

  Office({required this.name, required this.address, required this.image});

  factory Office.fromJson(Map<String, dynamic> json) => _$OfficeFromJson(json);

  Map<String, dynamic> toJson() => _$OfficeToJson(this);
}

生成类:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'models.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

OfficesList _$OfficesListFromJson(Map<String, dynamic> json) => OfficesList(
      offices: (json['offices'] as List<dynamic>)
          .map((e) => Office.fromJson(e as Map<String, dynamic>))
          .toList(),
    );

Map<String, dynamic> _$OfficesListToJson(OfficesList instance) =>
    <String, dynamic>{
      'offices': instance.offices,
    };

Office _$OfficeFromJson(Map<String, dynamic> json) => Office(
      name: json['name'] as String,
      address: json['address'] as String,
      image: json['image'] as String,
    );

Map<String, dynamic> _$OfficeToJson(Office instance) => <String, dynamic>{
      'name': instance.name,
      'address': instance.address,
      'image': instance.image,
    };
void main(){
  Office of1 = Office(name: "pupa", address: "lupa", image: "image");
  Office of2 = Office(name: "lupa", address: "pupa", image: "image");

  OfficesList list1 = OfficesList(offices: [of1,of2]);
  
  OfficesList list2 = OfficesList.fromJson(list1.toJson());

  print(list2.toJson());
}

这些行中的例外

OfficesList list2 = OfficesList.fromJson(list1.toJson())
.map((e) => Office.fromJson(e as Map<String, dynamic>))
Unhandled exception:
type 'Office' is not a subtype of type 'Map<String, dynamic>' in type cast
#0      _$OfficesListFromJson.<anonymous closure> (package:jsom_test/models.g.dart:11:41)
#1      MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
#2      ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
#3      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
#4      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#5      new List.of (dart:core-patch/array_patch.dart:51:28)
#6      ListIterable.toList (dart:_internal/iterable.dart:213:44)
#7      _$OfficesListFromJson (package:jsom_test/models.g.dart:12:12)
#8      new OfficesList.fromJson (package:jsom_test/models.dart:9:62)
#9      main (package:jsom_test/json_test.dart:9:35)
#10     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

I am trying to generate fromJSON and toJson functions for my nested objects using the json_serializable library. But when I tried to use the generated functions, I got an error with deserialization of the nested object.

I think I understand how to write toJSON and FromJSON functions. But I want to figure out how to work with the json_serializable library and work with the generated code.

Class with models:

import 'package:json_annotation/json_annotation.dart';
part 'models.g.dart';

@JsonSerializable()
class OfficesList {
  List<Office> offices;
  OfficesList({required this.offices});

  factory OfficesList.fromJson(Map<String, dynamic> json) => _$OfficesListFromJson(json);

  Map<String, dynamic> toJson() => _$OfficesListToJson(this);
}

@JsonSerializable()
class Office {
  final String name;
  final String address;
  final String image;

  Office({required this.name, required this.address, required this.image});

  factory Office.fromJson(Map<String, dynamic> json) => _$OfficeFromJson(json);

  Map<String, dynamic> toJson() => _$OfficeToJson(this);
}

Generated class:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'models.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

OfficesList _$OfficesListFromJson(Map<String, dynamic> json) => OfficesList(
      offices: (json['offices'] as List<dynamic>)
          .map((e) => Office.fromJson(e as Map<String, dynamic>))
          .toList(),
    );

Map<String, dynamic> _$OfficesListToJson(OfficesList instance) =>
    <String, dynamic>{
      'offices': instance.offices,
    };

Office _$OfficeFromJson(Map<String, dynamic> json) => Office(
      name: json['name'] as String,
      address: json['address'] as String,
      image: json['image'] as String,
    );

Map<String, dynamic> _$OfficeToJson(Office instance) => <String, dynamic>{
      'name': instance.name,
      'address': instance.address,
      'image': instance.image,
    };
void main(){
  Office of1 = Office(name: "pupa", address: "lupa", image: "image");
  Office of2 = Office(name: "lupa", address: "pupa", image: "image");

  OfficesList list1 = OfficesList(offices: [of1,of2]);
  
  OfficesList list2 = OfficesList.fromJson(list1.toJson());

  print(list2.toJson());
}

The exception in these lines

OfficesList list2 = OfficesList.fromJson(list1.toJson())
.map((e) => Office.fromJson(e as Map<String, dynamic>))
Unhandled exception:
type 'Office' is not a subtype of type 'Map<String, dynamic>' in type cast
#0      _$OfficesListFromJson.<anonymous closure> (package:jsom_test/models.g.dart:11:41)
#1      MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
#2      ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
#3      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
#4      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#5      new List.of (dart:core-patch/array_patch.dart:51:28)
#6      ListIterable.toList (dart:_internal/iterable.dart:213:44)
#7      _$OfficesListFromJson (package:jsom_test/models.g.dart:12:12)
#8      new OfficesList.fromJson (package:jsom_test/models.dart:9:62)
#9      main (package:jsom_test/json_test.dart:9:35)
#10     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文