json_serializable flutter parse并与钥匙连接与模态和映射器的映射

发布于 2025-02-13 04:50:23 字数 1683 浏览 0 评论 0 原文

Thais是我的JSON格式,


"recommended": {
            "section_title": "Recommended",
            "section_sub_title": "Recommended",
            "data": {
                "Allahabad": [
                    {
                        "gym_id": "9",
                        "name": "raga fitness",
                        "address": "35 Vivekanand Marg Hewett Road Allahabad",
                        "city_id": "1565",
                        "gym_logo": "http://139.180.218.66/gym/resources/alias_raga_fitness_20220403165957.jpeg",
                        "no_of_ratings": null,
                        "total_ratings": null,
                        "average_ratings": null,
                        "city_name": "Allahabad"
                    }
                ],
                "Lucknow": [
                    {
                        "gym_id": "2",
                        "name": "Gym Name 2",
                        "address": "gym address",
                        "city_id": "1496",
                        "gym_logo": "http://139.180.218.66/gym/resources/alias_Gym_Name_20220127182703.jpeg",
                        "no_of_ratings": "16",
                        "total_ratings": "55.5",
                        "average_ratings": "3.5",
                        "city_name": "Lucknow"
                    }
                ]
            }
        }

我使用JSON_SERIALIZABLE,DIO和JSON_ANNOTATION,希望用模态和响应信息映射

我正在使用json_serializable在Flutter中使用JSON_Serializable将类存储在文件中并从中读回。我不是为了简单起见,在这里发布原始类,但是原则是编写应用程序的一半,我决定要将可变名称“ at astupidname”更改为“名称”。我如何建议代码生成实用程序将JSON值分配给密钥“ AstupidName”,如果它存在于JSON中,则将其分配给变量“名称”,但是如果存在键“名称”以将其分配给变量,则IE在文件的较新版本中?

Thais is my json format


"recommended": {
            "section_title": "Recommended",
            "section_sub_title": "Recommended",
            "data": {
                "Allahabad": [
                    {
                        "gym_id": "9",
                        "name": "raga fitness",
                        "address": "35 Vivekanand Marg Hewett Road Allahabad",
                        "city_id": "1565",
                        "gym_logo": "http://139.180.218.66/gym/resources/alias_raga_fitness_20220403165957.jpeg",
                        "no_of_ratings": null,
                        "total_ratings": null,
                        "average_ratings": null,
                        "city_name": "Allahabad"
                    }
                ],
                "Lucknow": [
                    {
                        "gym_id": "2",
                        "name": "Gym Name 2",
                        "address": "gym address",
                        "city_id": "1496",
                        "gym_logo": "http://139.180.218.66/gym/resources/alias_Gym_Name_20220127182703.jpeg",
                        "no_of_ratings": "16",
                        "total_ratings": "55.5",
                        "average_ratings": "3.5",
                        "city_name": "Lucknow"
                    }
                ]
            }
        }

i use json_serializable, dio, and json_annotation want to be mapped with modal and ResponseMapper

I am using json_serializable in Flutter to store a class in a file and read back from it. I am not posting the original class here for simplicity, but the principle is that half way through writing the app I decided that I wanted to change the variable name "aStupidName" to "name". How can I advise the code generation utility to assign the JSON value with the key "aStupidName", if it exists in the JSON, to the variable "name", but if the key "name" exists to assign this to the variable instead, i.e. in newer versions of the file?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2025-02-20 04:50:23

解决方案1

在模型类中,假设 xyz ,使用 @jsonkey 映射一个从JSON读取字段的方式。这可以用 /a>进行2个参数:json数据作为 map< string,动态> 和字段名称。这将是以下内容。另外,

import 'package:json_annotation/json_annotation.dart';

part 'xyz.g.dart';

Object? mapper(json, field) => json['name'] ?? json['aStupidName'];

@JsonSerializable(explicitToJson: true)
class XYZ {
  @JsonKey(readValue: mapper)
  String name;
  XYZ({required this.name});

  factory XYZ.fromJson(Map<String, dynamic> json) => _$XYZFromJson(json);
  Map<String, dynamic> toJson() => _$XYZToJson(this);
}

我还建议将API/DB/正常化为标准化,以始终为 name ,因此无需以这种方式注释。

解决方案2

另一方面, 可以像以下内容一样通过手工实现序列化/避难所化。只需使用操作员条件表达式 名称 astupidname 之间。

@JsonSerializable()
class XYZ {

  final String name;

  ...

  XYZ.fromJson(Map<String, dynamic> json)
    : name = json['name'] ?? json['aStupidName'],
    ...;

  Map<String, dynamic> toJson() => {
      'name': name,
      ...
  };
}

Solution 1

In the model class, let's say XYZ, use the @JsonKey to map a how a field is read from JSON. This can be done with JsonKey.readValue that takes 2 arguments: the JSON data as Map<String, dynamic> and the field name. It's going to be something like the following. Also, check out the official docs.

import 'package:json_annotation/json_annotation.dart';

part 'xyz.g.dart';

Object? mapper(json, field) => json['name'] ?? json['aStupidName'];

@JsonSerializable(explicitToJson: true)
class XYZ {
  @JsonKey(readValue: mapper)
  String name;
  XYZ({required this.name});

  factory XYZ.fromJson(Map<String, dynamic> json) => _$XYZFromJson(json);
  Map<String, dynamic> toJson() => _$XYZToJson(this);
}

I'd also recommend normalizing the API/DB/whatever itself to be always name so there is no need to annotate this way.

Solution 2

On the other hand, it's possible to implement the serialization/deserialization by hand like the following. Just use the ?? operator conditional expression between name and aStupidName.

@JsonSerializable()
class XYZ {

  final String name;

  ...

  XYZ.fromJson(Map<String, dynamic> json)
    : name = json['name'] ?? json['aStupidName'],
    ...;

  Map<String, dynamic> toJson() => {
      'name': name,
      ...
  };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文