0

主要问题:如何使用 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 inlace 的简单示例:

  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”]

4

2 回答 2

0

这是最简单的方法!

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

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

于 2020-04-15T20:25:15.083 回答
0

使用简单的 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"]"

对于自定义模型类:

        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);
于 2020-04-16T03:34:07.693 回答