Home > Software engineering >  Unable to parse Json response to Dart
Unable to parse Json response to Dart

Time:09-12

I want to parse a JSON response to dart and I have used various online converts and plugins to do that but nothing is working. I need to display the list of the below data. please help.

{
    "status": "success",
    "message": "Data Found",
    "data": {
        "current_page": 1,
        "data": [
            {
                "id": 61,
                "name": "Goldy",
                "photo": "/obituaries_files/images/Goldy_4e9ceb6b0b3eecc77006f6753e05c817_20220819025324.png",
                "sunrise_date": "2022-06-15",
                "sunset_date": "2022-07-12"
            },
            {
                "id": 75,
                "name": "Silver 10-0",
                "photo": "/obituaries_files/images/Silver_10-0_d5cac8d7f67d675ca94a8face0ff47f0_20220720014910.gif",
                "sunrise_date": "2022-06-15",
                "sunset_date": "2022-07-20"
            },
            {
                "id": 195,
                "name": "John Martin",
                "photo": "/obituaries_files/images/John_Martin_342d5bb70be0b6f25bf50a40fb088b5c_20220722013325.gif",
                "sunrise_date": "1984-06-07",
                "sunset_date": "2022-07-20"
            },
            {
                "id": 200,
                "name": "Tyson",
                "photo": "/obituaries_files/images/New_30ef344d651f7127ebd1ee18a0592f22_20220721050721.jpeg",
                "sunrise_date": "2022-07-12",
                "sunset_date": "2022-07-21"
            },
            {
                "id": 213,
                "name": "Gold Fish",
                "photo": "/obituaries_files/images/Gold_Fish_99af00a6905ed2c958371a3105d90c4e_20220721054534.jpeg",
                "sunrise_date": "2022-06-15",
                "sunset_date": "2022-07-13"
            },
            {
                "id": 214,
                "name": "Test",
                "photo": "/obituaries_files/images/Test_fc1d82c1c421f049598edbb6ef1f8ae5_20220721055340.gif",
                "sunrise_date": "2022-06-15",
                "sunset_date": "2022-07-20"
            },
            {
                "id": 220,
                "name": "Anna Fritz",
                "photo": "/obituaries_files/images/Anna_Fritz_0ef99c9e429775b87a4957ded8c6e78a_20220721060536.jpeg",
                "sunrise_date": "2022-06-15",
                "sunset_date": "2022-07-21"
            },
            {
                "id": 250,
                "name": "Silver",
                "photo": "/obituaries_files/images/Silver_d7b5f795c65082e89bb8cca065622a7f_20220722011601.jpg",
                "sunrise_date": "1967-07-12",
                "sunset_date": "2022-06-16"
            },
            {
                "id": 252,
                "name": "Test",
                "photo": "/obituaries_files/images/Test_a04809ee2e677a35b1f28479f852d46f_20220722012306.jpeg",
                "sunrise_date": "2022-07-21",
                "sunset_date": "2022-07-21"
            },
            {
                "id": 254,
                "name": "Basic",
                "photo": "/obituaries_files/images/Basic_e28a078946c99b1c76e7abf5fee7c46f_20220722012922.gif",
                "sunrise_date": "2022-07-13",
                "sunset_date": "2022-07-13"
            }
        ],
        "first_page_url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=1",
        "from": 1,
        "last_page": 5,
        "last_page_url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=5",
        "links": [
            {
                "url": null,
                "label": "« Previous",
                "active": false
            },
            {
                "url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=1",
                "label": "1",
                "active": true
            },
            {
                "url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=2",
                "label": "2",
                "active": false
            },
            {
                "url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=3",
                "label": "3",
                "active": false
            },
            {
                "url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=4",
                "label": "4",
                "active": false
            },
            {
                "url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=5",
                "label": "5",
                "active": false
            },
            {
                "url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=2",
                "label": "Next »",
                "active": false
            }
        ],
        "next_page_url": "http://obituary.krescentglobal.com/api/get_obituaries_list?page=2",
        "path": "http://obituary.krescentglobal.com/api/get_obituaries_list",
        "per_page": 10,
        "prev_page_url": null,
        "to": 10,
        "total": 49
    }
}

CodePudding user response:

class Autogenerated {
  String? status;
  String? message;
  Data? data;

  Autogenerated({this.status, this.message, this.data});

  Autogenerated.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    message = json['message'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }

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

class Data {
  int? currentPage;
  List<Data>? data;
  String? firstPageUrl;
  int? from;
  int? lastPage;
  String? lastPageUrl;
  List<Links>? links;
  String? nextPageUrl;
  String? path;
  int? perPage;
  Null? prevPageUrl;
  int? to;
  int? total;

  Data(
      {this.currentPage,
      this.data,
      this.firstPageUrl,
      this.from,
      this.lastPage,
      this.lastPageUrl,
      this.links,
      this.nextPageUrl,
      this.path,
      this.perPage,
      this.prevPageUrl,
      this.to,
      this.total});

  Data.fromJson(Map<String, dynamic> json) {
    currentPage = json['current_page'];
    if (json['data'] != null) {
      data = <Data>[];
      json['data'].forEach((v) {
        data!.add(new Data.fromJson(v));
      });
    }
    firstPageUrl = json['first_page_url'];
    from = json['from'];
    lastPage = json['last_page'];
    lastPageUrl = json['last_page_url'];
    if (json['links'] != null) {
      links = <Links>[];
      json['links'].forEach((v) {
        links!.add(new Links.fromJson(v));
      });
    }
    nextPageUrl = json['next_page_url'];
    path = json['path'];
    perPage = json['per_page'];
    prevPageUrl = json['prev_page_url'];
    to = json['to'];
    total = json['total'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['current_page'] = this.currentPage;
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    data['first_page_url'] = this.firstPageUrl;
    data['from'] = this.from;
    data['last_page'] = this.lastPage;
    data['last_page_url'] = this.lastPageUrl;
    if (this.links != null) {
      data['links'] = this.links!.map((v) => v.toJson()).toList();
    }
    data['next_page_url'] = this.nextPageUrl;
    data['path'] = this.path;
    data['per_page'] = this.perPage;
    data['prev_page_url'] = this.prevPageUrl;
    data['to'] = this.to;
    data['total'] = this.total;
    return data;
  }
}

class Data {
  int? id;
  String? name;
  String? photo;
  String? sunriseDate;
  String? sunsetDate;

  Data({this.id, this.name, this.photo, this.sunriseDate, this.sunsetDate});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    photo = json['photo'];
    sunriseDate = json['sunrise_date'];
    sunsetDate = json['sunset_date'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['photo'] = this.photo;
    data['sunrise_date'] = this.sunriseDate;
    data['sunset_date'] = this.sunsetDate;
    return data;
  }
}

class Links {
  String? url;
  String? label;
  bool? active;

  Links({this.url, this.label, this.active});

  Links.fromJson(Map<String, dynamic> json) {
    url = json['url'];
    label = json['label'];
    active = json['active'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['url'] = this.url;
    data['label'] = this.label;
    data['active'] = this.active;
    return data;
  }
}

CodePudding user response:

Use this json to dart converter

class ApiData {
  String? status;
  String? message;
  Data? data;

  ApiData({this.status, this.message, this.data});

  ApiData.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    message = json['message'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }

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

class Data {
  int? currentPage;
  List<Data>? data;
  String? firstPageUrl;
  int? from;
  int? lastPage;
  String? lastPageUrl;
  List<Links>? links;
  String? nextPageUrl;
  String? path;
  int? perPage;
  String? prevPageUrl;
  int? to;
  int? total;

  Data(
      {this.currentPage,
      this.data,
      this.firstPageUrl,
      this.from,
      this.lastPage,
      this.lastPageUrl,
      this.links,
      this.nextPageUrl,
      this.path,
      this.perPage,
      this.prevPageUrl,
      this.to,
      this.total});

  Data.fromJson(Map<String, dynamic> json) {
    currentPage = json['current_page'];
    if (json['data'] != null) {
      data = <Data>[];
      json['data'].forEach((v) {
        data!.add(new Data.fromJson(v));
      });
    }
    firstPageUrl = json['first_page_url'];
    from = json['from'];
    lastPage = json['last_page'];
    lastPageUrl = json['last_page_url'];
    if (json['links'] != null) {
      links = <Links>[];
      json['links'].forEach((v) {
        links!.add(new Links.fromJson(v));
      });
    }
    nextPageUrl = json['next_page_url'];
    path = json['path'];
    perPage = json['per_page'];
    prevPageUrl = json['prev_page_url'];
    to = json['to'];
    total = json['total'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['current_page'] = this.currentPage;
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    data['first_page_url'] = this.firstPageUrl;
    data['from'] = this.from;
    data['last_page'] = this.lastPage;
    data['last_page_url'] = this.lastPageUrl;
    if (this.links != null) {
      data['links'] = this.links!.map((v) => v.toJson()).toList();
    }
    data['next_page_url'] = this.nextPageUrl;
    data['path'] = this.path;
    data['per_page'] = this.perPage;
    data['prev_page_url'] = this.prevPageUrl;
    data['to'] = this.to;
    data['total'] = this.total;
    return data;
  }
}

class Data {
  int? id;
  String? name;
  String? photo;
  String? sunriseDate;
  String? sunsetDate;

  Data({this.id, this.name, this.photo, this.sunriseDate, this.sunsetDate});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    photo = json['photo'];
    sunriseDate = json['sunrise_date'];
    sunsetDate = json['sunset_date'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['photo'] = this.photo;
    data['sunrise_date'] = this.sunriseDate;
    data['sunset_date'] = this.sunsetDate;
    return data;
  }
}

class Links {
  String? url;
  String? label;
  bool? active;

  Links({this.url, this.label, this.active});

  Links.fromJson(Map<String, dynamic> json) {
    url = json['url'];
    label = json['label'];
    active = json['active'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['url'] = this.url;
    data['label'] = this.label;
    data['active'] = this.active;
    return data;
  }
}
  • Related