【问题标题】:Is it possible to parse json in Flutter/Dart with missing attributes (keys of json data)?是否可以在 Flutter/Dart 中解析缺少属性(json 数据的键)的 json?
【发布时间】:2021-08-13 19:33:09
【问题描述】:

我有来自 API 的 json 形式的对象列表。这些对象有一些属性,问题是一些对象缺少属性。 我的问题是: 是否可以在 Flutter/Dart 中解析这些数据? 我必须更改为 API 响应,因此其中没有缺少任何属性。

JSON 数据:

[
    {
        "names": "Jean lewis  Mbayabu ",
        "users_name": "jeanlewis",
        "avt": "Image URL",
        "id": "32",
        "title": "Canada",
        "category": "student"
    },
    {
        "names": "Abhi Vish",
        "users_name": "abhi",
        "avt": "Image URL",
        "id": "59",
        "category": "student"
    },
    {
        "names": "Julie Mabengi",
        "users_name": "julie",
        "avt": "Image URL",
        "id": "116"
    }
]

【问题讨论】:

  • 当然,您只需要处理给定的key 确实返回null 的情况(例如,使该字段可以为空)。
  • @julemand101 我正在尝试像这样解析数据:SearchedUser.fromJson(dynamic response) { return SearchedUser( userId: response['id'], name: response['names'], userName: response['users_name'], profileImage: response['avt'], title: response['title'], category: response['category'], ); } 你能告诉我一个例子,我该如何处理null 检查key
  • title: response['title'] ?? 'default title', category: response['category'] ?? 'default category',

标签: json flutter dart


【解决方案1】:

这里是一个如何做的例子。我已将 titlecategory 设为可空,因为我们并不总是有这两个字段:

import 'dart:convert';

const jsonString = '''
[
    {
        "names": "Jean lewis  Mbayabu ",
        "users_name": "jeanlewis",
        "avt": "Image URL",
        "id": "32",
        "title": "Canada",
        "category": "student"
    },
    {
        "names": "Abhi Vish",
        "users_name": "abhi",
        "avt": "Image URL",
        "id": "59",
        "category": "student"
    },
    {
        "names": "Julie Mabengi",
        "users_name": "julie",
        "avt": "Image URL",
        "id": "116"
    }
]
''';

class SearchedUser {
  final String names;
  final String users_name;
  final String avt;
  final int id;
  final String? title;
  final String? category;

  const SearchedUser(
      {required this.names,
      required this.users_name,
      required this.avt,
      required this.id,
      required this.title,
      required this.category});

  factory SearchedUser.fromJson(Map<String, dynamic> json) => SearchedUser(
      names: json['names'] as String,
      users_name: json['users_name'] as String,
      avt: json['avt'] as String,
      id: int.parse(json['id'] as String),
      title: json['title'] as String?,
      // title: json['title'] ?? "defaultTitle", // default title if field is absent or null
      category: json['category'] as String?);
      // category: json['category'] ?? "defaultCategory"); // default category if field is absent or null

  @override
  String toString() => 'names: $names, '
      'users_name: $users_name, '
      'avt: $avt, '
      'id: $id, '
      'title: $title, '
      'category: $category';
}

void main() {
  final jsonList = jsonDecode(jsonString) as List<dynamic>;
  final searchedUsers = [
    for (final map in jsonList.cast<Map<String, dynamic>>())
      SearchedUser.fromJson(map)
  ];

  searchedUsers.forEach(print);
  // names: Jean lewis  Mbayabu , users_name: jeanlewis, avt: Image URL, id: 32, title: Canada, category: student
  // names: Abhi Vish, users_name: abhi, avt: Image URL, id: 59, title: null, category: student
  // names: Julie Mabengi, users_name: julie, avt: Image URL, id: 116, title: null, category: null
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-25
    • 2021-02-09
    • 2021-07-10
    • 2022-01-09
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2021-01-03
    相关资源
    最近更新 更多