1

I tried to convert json below to dart class with many online converters also. But when i implemented that code dose not work and give me some error.

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
                }
            ]
        }
    ]
}
desertnaut
  • 57,590
  • 26
  • 140
  • 166
GHUSN Mhsen
  • 13
  • 1
  • 1
  • 4

2 Answers2

7

You can covert it using this website , I use it always and it works fine This is you json code after been converted to a dart class :

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,
    };
}
Aimen SAYOUD
  • 275
  • 3
  • 5
0

It is very simple, you can convert your json to dart online editor using this website

as you have given your json response, you can paste on json to dart editor and you can write className. i supposed Model Name is 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;
  }
}

if you want to generate json to dart using different method, you may look this methods too. Using the json_serializable package and json_annotation here is detail tutorial

Karamat Subhani
  • 104
  • 1
  • 7