如何在dart中将Json转换为模型类?

问题描述 投票:0回答:3

我也尝试使用许多在线转换器将下面的 json 转换为 dart 类。但是当我实现该代码时,该代码不起作用并给我一些错误。

json:

{
    "sections": [
        {
            "id": "863",
            "title": "الفصل الاول (1)",
            "course_id": 110011,
            "description": "",
            "order": "2",
            "percent": 16.666666666666664,
            "items": [
                {
                    "id": 110167,
                    "type": "lp_quiz",
                    "title": "الإختبار",
                    "preview": false,
                    "duration": "01 hours",
                    "graduation": "failed",
                    "status": "completed",
                    "locked": false
                }
            ]
        }
    ]
}
json flutter dart
3个回答
10
投票

您可以使用这个网站隐藏它,我一直使用它并且效果很好 这是转换为 dart 类后的 json 代码 :

import 'dart:convert';

ModelClass modelClassFromJson(String str) => ModelClass.fromJson(json.decode(str));

String modelClassToJson(ModelClass data) => json.encode(data.toJson());

class ModelClass {
    ModelClass({
        this.sections,
    });

    List<Section> sections;

    factory ModelClass.fromJson(Map<String, dynamic> json) => ModelClass(
        sections: List<Section>.from(json["sections"].map((x) => Section.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "sections": List<dynamic>.from(sections.map((x) => x.toJson())),
    };
}

class Section {
    Section({
        this.id,
        this.title,
        this.courseId,
        this.description,
        this.order,
        this.percent,
        this.items,
    });

    String id;
    String title;
    int courseId;
    String description;
    String order;
    double percent;
    List<Item> items;

    factory Section.fromJson(Map<String, dynamic> json) => Section(
        id: json["id"],
        title: json["title"],
        courseId: json["course_id"],
        description: json["description"],
        order: json["order"],
        percent: json["percent"].toDouble(),
        items: List<Item>.from(json["items"].map((x) => Item.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "title": title,
        "course_id": courseId,
        "description": description,
        "order": order,
        "percent": percent,
        "items": List<dynamic>.from(items.map((x) => x.toJson())),
    };
}

class Item {
    Item({
        this.id,
        this.type,
        this.title,
        this.preview,
        this.duration,
        this.graduation,
        this.status,
        this.locked,
    });

    int id;
    String type;
    String title;
    bool preview;
    String duration;
    String graduation;
    String status;
    bool locked;

    factory Item.fromJson(Map<String, dynamic> json) => Item(
        id: json["id"],
        type: json["type"],
        title: json["title"],
        preview: json["preview"],
        duration: json["duration"],
        graduation: json["graduation"],
        status: json["status"],
        locked: json["locked"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "type": type,
        "title": title,
        "preview": preview,
        "duration": duration,
        "graduation": graduation,
        "status": status,
        "locked": locked,
    };
}

1
投票

非常简单,你可以使用这个网站

将你的json转换为dart在线编辑器

当您给出 json 响应时,您可以将 json 粘贴到 dart 编辑器,然后编写 className。我猜模型名称是

DartJsonModel

class DartJsonModel {
  List<Sections>? sections;

  DartJsonModel({this.sections});

  DartJsonModel.fromJson(Map<String, dynamic> json) {
    if (json['sections'] != null) {
      sections = <Sections>[];
      json['sections'].forEach((v) {
        sections!.add(new Sections.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.sections != null) {
      data['sections'] = this.sections!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Sections {
  String? id;
  String? title;
  int? courseId;
  String? description;
  String? order;
  double? percent;
  List<Items>? items;

  Sections(
      {this.id,
      this.title,
      this.courseId,
      this.description,
      this.order,
      this.percent,
      this.items});

  Sections.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    title = json['title'];
    courseId = json['course_id'];
    description = json['description'];
    order = json['order'];
    percent = json['percent'];
    if (json['items'] != null) {
      items = <Items>[];
      json['items'].forEach((v) {
        items!.add(new Items.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['title'] = this.title;
    data['course_id'] = this.courseId;
    data['description'] = this.description;
    data['order'] = this.order;
    data['percent'] = this.percent;
    if (this.items != null) {
      data['items'] = this.items!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Items {
  int? id;
  String? type;
  String? title;
  bool? preview;
  String? duration;
  String? graduation;
  String? status;
  bool? locked;

  Items(
      {this.id,
      this.type,
      this.title,
      this.preview,
      this.duration,
      this.graduation,
      this.status,
      this.locked});

  Items.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    type = json['type'];
    title = json['title'];
    preview = json['preview'];
    duration = json['duration'];
    graduation = json['graduation'];
    status = json['status'];
    locked = json['locked'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['type'] = this.type;
    data['title'] = this.title;
    data['preview'] = this.preview;
    data['duration'] = this.duration;
    data['graduation'] = this.graduation;
    data['status'] = this.status;
    data['locked'] = this.locked;
    return data;
  }
}

如果你想使用不同的方法生成 json 到 dart,你也可以看看这个方法。 使用 json_serialized 包json_annotation 这是详细信息教程


0
投票

您可以下载任何插件,如“json to dart”,直接在 IDE 中转换,或者您可以使用 this 网站

© www.soinside.com 2019 - 2024. All rights reserved.