Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int数组怎么转, "Nums?": "$[]int",这样不行啊 #66

Open
keyoumi opened this issue Jan 29, 2024 · 2 comments
Open

int数组怎么转, "Nums?": "$[]int",这样不行啊 #66

keyoumi opened this issue Jan 29, 2024 · 2 comments

Comments

@keyoumi
Copy link

keyoumi commented Jan 29, 2024

"Nums?": "$[]num",这样也不行啊

@CurryPaste
Copy link

CurryPaste commented Jun 20, 2024

用jsonKey

{
  "@meta": {
    "import": [],
    "comments": {
      "name": "名字",
      "following": "关注",
      "followers": "粉丝",
      "keywords": "标签",
      "age": "年龄"
    },
    "nullable": true,
    "ignore": false
  },
  "@JsonKey(name: 'thisIsApiKey') List<int>?": "thisIsIntList",
  "name": "wendux",
  "following": "$[]user",
  "followers": "$[]user",
  "keywords": "$[]String",
  "age?": 20
}

@CurryPaste
Copy link

会生成
user.dart

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User {
  User();

  @JsonKey(name: 'thisIsApiKey') List<int>? thisIsIntList;
  // 名字
  String? name;
  // 关注
  List<User>? following;
  // 粉丝
  List<User>? followers;
  // 标签
  List<String>? keywords;
  // 年龄
  num? age;
  
  factory User.fromJson(Map<String,dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}

user.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'user.dart';

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

User _$UserFromJson(Map<String, dynamic> json) => User()
  ..thisIsIntList = (json['thisIsApiKey'] as List<dynamic>?)
      ?.map((e) => (e as num).toInt())
      .toList()
  ..name = json['name'] as String?
  ..following = (json['following'] as List<dynamic>?)
      ?.map((e) => User.fromJson(e as Map<String, dynamic>))
      .toList()
  ..followers = (json['followers'] as List<dynamic>?)
      ?.map((e) => User.fromJson(e as Map<String, dynamic>))
      .toList()
  ..keywords =
      (json['keywords'] as List<dynamic>?)?.map((e) => e as String).toList()
  ..age = json['age'] as num?;

Map<String, dynamic> _$UserToJson(User instance) => <String, dynamic>{
      'thisIsApiKey': instance.thisIsIntList,
      'name': instance.name,
      'following': instance.following,
      'followers': instance.followers,
      'keywords': instance.keywords,
      'age': instance.age,
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants