有什么方法可以在 dart 中使用 JsonSerializable 动态创建 JsonKey,我们可以从 JsonSerializable class 创建和获取数组吗?

Is there any way to create JsonKey dynamic using JsonSerializable in dart and can we create and get only array from the JsonSerializable class?

主要问题:如何使用json_serializable解析这个json数组?

  [
  {
    "albumId": 1,
    "id": 1,
    "title": "accusamus beatae ad facilis cum similique qui sunt",
    "url": "http://placehold.it/600/92c952",
    "thumbnailUrl": "http://placehold.it/150/92c952"
  },
  {
    "albumId": 1,
    "id": 2,
    "title": "reprehenderit est deserunt velit ipsam",
    "url": "http://placehold.it/600/771796",
    "thumbnailUrl": "http://placehold.it/150/771796"
  },
  {
    "albumId": 1,
    "id": 3,
    "title": "officia porro iure quia iusto qui ipsa ut modi",
    "url": "http://placehold.it/600/24f355",
    "thumbnailUrl": "http://placehold.it/150/24f355"
  }
]

使用自定义对象的 StringList 镶嵌的简单示例:

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

  @JsonSerializable()
  class AllJsonData {
    @JsonKey(name: 'some_key') // @Question: Can we make this some_key dynamic
    List<String> orders;

    AllJsonData(this.orders);

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

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

all_json.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'all_json.dart';

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

AllJsonData _$AllJsonDataFromJson(Map<String, dynamic> json) {
  return AllJsonData(
    (json['some_key'] as List)?.map((e) => e as String)?.toList(),
  );
}

Map<String, dynamic> _$AllJsonDataToJson(AllJsonData instance) =>
    <String, dynamic>{
      'some_key': instance.orders,
    };

@Question1:我们可以让这个some_key动态吗?创建对象并反序列化后,它给出的结果是这样的。

    AllJsonData allJsonData = AllJsonData(['Some', 'People']);
    String vv = jsonEncode(allJsonData.toJson());
    print(vv);

    // Getting output like:
    "{"education":["Some","People"]}"

    // Required Output only array without key:
    ["Some","People"]

@Question2: 如何在没有键的情况下得到类似数组的输出: ["Some","People"]

这是最简单的方法!

final list = (jsonDecode(text) as List).map((e) => Album.fromJson(e)).toList()

JsonSerializable 假设所有对象都是 Map<String, dynamic>。如果您想在顶层处理列表,请执行我在此处所做的操作。

使用简单的 String[] 将模型从 JsonArray 转换为 String。

        AllJsonData allJsonData = AllJsonData(
          orders: ['James', 'Bipin', 'Simon'],
        );
        String jsonObjectAsString = jsonEncode(allJsonData.toJson());
        var parentMap = jsonDecode(jsonObjectAsString);

        // @read: To convert as JsonString
        var jsonString = jsonEncode(parentMap['orders'],
            toEncodable: (e) => e.toString());
        print(jsonObjectAsString); 
       // gives result like "["James","Bipin","Simon"]"

自定义模型 class:

        AllJsonData allJsonData = AllJsonData(
          education: edu,
        );
        String jsonObjectAsString = jsonEncode(allJsonData.toJson());
        var parentMap = jsonDecode(jsonObjectAsString);
        var subParentMap = parentMap['education'];
        List<dynamic> mList = subParentMap['modelList'];
        // mapping as model-list
        var modelList =
            mList.map((e) => ExpPopupModel.fromJson(e)).toList();
        // convert into json-string
        var jsonString = jsonEncode(subParentMap['modelList'],
            toEncodable: (e) => e.toString());
        print(jsonObjectAsString);