使用 Flutter 中的类解析 JSON 文件

Posted

技术标签:

【中文标题】使用 Flutter 中的类解析 JSON 文件【英文标题】:Parse a JSON file using class in Flutter 【发布时间】:2021-06-12 06:40:29 【问题描述】:

我有以下问题。我只是无法理解将信息从 JSON 解析为颤振对象数组的最简单方法。

我有以下课程:

class Organization 
  String subject;
  String organization;
  String shortDescription;
  String address;
  String phoneNumber;
  String contactInfo;
  List<String> tags;

  Organization(
    this.subject,
    this.organization,
    this.shortDescription,
    this.address,
    this.phoneNumber,
    this.contactInfo,
    this.tags,
  );

  Organization.fromJson(Map<String, dynamic> json) 
    subject = json['Subject'];
    organization = json['Organization'];
    shortDescription = json['Short description'];
    address = json['Address'];
    phoneNumber = json['Phone number'];
    contactInfo = json['Contact info'];
    tags = json['Tags'].cast<String>();
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Subject'] = this.subject;
    data['Organization'] = this.organization;
    data['Short description'] = this.shortDescription;
    data['Address'] = this.address;
    data['Phone number'] = this.phoneNumber;
    data['Contact info'] = this.contactInfo;
    data['Tags'] = this.tags;
    return data;
  

这是我在资产文件夹中的 Json 文件的小样本。

[
  
    "Subject": "Cultuur - Culture ",
    "Organization": "Museum aan de stroom ",
    "Short description": "The MAS is a museum that provides you with permanent and temporary exhibitions. These offer a refreshing look at the city and it's history. At the top, on the tenth floor, you can enjoy a 360-degree panorama of the city, the port and the river.",
    "Address": "Hanzestedenplaats 1, 2000 Antwerpen",
    "Phone number": "+32 3 338 44 00",
    "Contact info": "www.mas.be",
    "Tags": ["Meeting place", "Museum", "Antwerp", "Art"]
  ,
  
    "Subject": "Cultuur - Culture ",
    "Organization": "Red star line museum ",
    "Short description": "Through personal story's of immigrants the Red star line museum paints a picture of the immigration history in Antwerp. ",
    "Address": "Montevideostraat 3, 2000 Antwerpen",
    "Phone number": "+32 3 298 27 70",
    "Contact info": "www.redstarline.be",
    "Tags": ["Museum", "Antwerp", "History", "Immigration"]
  
]

我只是想将此文件中的信息解析为如下所示的组织列表:

List<Organisation> organisations = [];

  Future<void> readJson() async 
    final String response =
        await rootBundle.loadString('assets/organisations.json');
    final data = await json.decode(response);
    organisations = data;
  

但是我得到 Unhandled Exception: type List dynamic is not a subtype of type List Organization 错误。

在我看来,数据应该是我的对象数组。我究竟做错了什么?或者是否有更简单的方法来实现这个结果?

【问题讨论】:

【参考方案1】:

readJson函数的最后,你可以这样调用Organization.fromJson

Future<void> _readJson() async 
    final String response =
        await rootBundle.loadString('assets/data/organizations.json');
    final List<dynamic> data = await json.decode(response);

    for (dynamic it in data) 
      final Organization organization = Organization.fromJson(it); // Parse data    
      organizations.add(organization); // and organization to List
    
  

【讨论】:

并且不要忘记将组织更改为组织

以上是关于使用 Flutter 中的类解析 JSON 文件的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:如何解析超复杂的 json 文件?

如何使用从 Flutter 中的 json 解析的嵌套映射列表中的函数创建对象

flutter 解析本地json

Flutter 中的 JSON 数据解析

Flutter 中的 JSON 解析和 Map

Flutter 从本地化 JSON 文件解析数组