I would need assistance in serializing the below JSON Response. By far, I've managed to only extract the district property but cannot seem to understand how to go about with the properties of ward_no. The code for what I've done is below. Please note that I would like to serialize the JSON response in the same approach as on my code as I found it nearly impossible to wrap my head around the Serializing techniques suggested in the Official Documents.

The Response:

[
    {
        "district": "Kolkata",    //I've only managed to extract this.
        "ward_no": [
            {
                "ward": "6",     
                "grievance": [
                    {
                        "serial_number": "0001",
                        "name": "Siddhartha Chatterjee"
                    },
                    {
                        "serial_number": "0002",
                        "name": "Sajujjo Ghosh"
                    }
                ],
                "general": [
                    {
                        "serial_number": "0003",
                        "name": "Dr. Partha Pratim Paul"
                    },
                    {
                        "serial_number": "0004",
                        "name": "Dr. Partha Pratim Paul"
                    }
                ],
                "urgent": [
                    {
                        "serial_number": "0005",
                        "name": "Ritwick Banerjee"
                    },
                    {
                        "serial_number": "0006",
                        "name": "Soumadip Banerjee"
                    }
                ],
                "services": [
                    {
                        "serial_number": "0007",
                        "name": "Tanajeet Biswas"
                    },
                    {
                        "serial_number": "0008",
                        "name": "Durba Chatterjee"
                    }
                ]
            }
        ]
    }
]

The code:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class Model {
  final String district;

  Model({required this.district});
}

class ModelClass with ChangeNotifier {
  List<dynamic> _data = [];

  List<dynamic> get data {
    return [..._data];
  }

  Future<void> getData() async {
    final url = Uri.http('192.168.0.6:3007', '/fetchData');
    final response = await http.get(url);
    print(response);
    final extractedData = json.decode(response.body);
    print(extractedData);
    Map<String, Model> temp = {};
    extractedData.forEach((value) {
      temp.putIfAbsent(
          value['_id'],
          () =>
              Model(district: value['district']));
    });
    print(temp);
    _data = temp.values.toList();
    print(_data);
  }
}

P.S: I'm not looking to use boilerplate code generated by those JSON to Dart converter links


Solution 1: Khadga shrestha

You can use this tools which will generate data class form the provided json https://jsontodart.zariman.dev/

class Response {
    String? district;
    List<WardNo>? wardNo;

    Response({this.district, this.wardNo});

    Response.fromJson(Map<String, dynamic> json) {
        this.district = json["district"];
        this.wardNo = json["ward_no"]==null ? null : (json["ward_no"] as List).map((e)=>WardNo.fromJson(e)).toList();
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data["district"] = this.district;
        if(this.wardNo != null)
            data["ward_no"] = this.wardNo?.map((e)=>e.toJson()).toList();
        return data;
    }
}

class WardNo {
    String? ward;
    List<Grievance>? grievance;
    List<General>? general;
    List<Urgent>? urgent;
    List<Services>? services;

    WardNo({this.ward, this.grievance, this.general, this.urgent, this.services});

    WardNo.fromJson(Map<String, dynamic> json) {
        this.ward = json["ward"];
        this.grievance = json["grievance"]==null ? null : (json["grievance"] as List).map((e)=>Grievance.fromJson(e)).toList();
        this.general = json["general"]==null ? null : (json["general"] as List).map((e)=>General.fromJson(e)).toList();
        this.urgent = json["urgent"]==null ? null : (json["urgent"] as List).map((e)=>Urgent.fromJson(e)).toList();
        this.services = json["services"]==null ? null : (json["services"] as List).map((e)=>Services.fromJson(e)).toList();
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data["ward"] = this.ward;
        if(this.grievance != null)
            data["grievance"] = this.grievance?.map((e)=>e.toJson()).toList();
        if(this.general != null)
            data["general"] = this.general?.map((e)=>e.toJson()).toList();
        if(this.urgent != null)
            data["urgent"] = this.urgent?.map((e)=>e.toJson()).toList();
        if(this.services != null)
            data["services"] = this.services?.map((e)=>e.toJson()).toList();
        return data;
    }
}

class Services {
    String? serialNumber;
    String? name;

    Services({this.serialNumber, this.name});

    Services.fromJson(Map<String, dynamic> json) {
        this.serialNumber = json["serial_number"];
        this.name = json["name"];
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data["serial_number"] = this.serialNumber;
        data["name"] = this.name;
        return data;
    }
}

class Urgent {
    String? serialNumber;
    String? name;

    Urgent({this.serialNumber, this.name});

    Urgent.fromJson(Map<String, dynamic> json) {
        this.serialNumber = json["serial_number"];
        this.name = json["name"];
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data["serial_number"] = this.serialNumber;
        data["name"] = this.name;
        return data;
    }
}

class General {
    String? serialNumber;
    String? name;

    General({this.serialNumber, this.name});

    General.fromJson(Map<String, dynamic> json) {
        this.serialNumber = json["serial_number"];
        this.name = json["name"];
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data["serial_number"] = this.serialNumber;
        data["name"] = this.name;
        return data;
    }
}

class Grievance {
    String? serialNumber;
    String? name;

    Grievance({this.serialNumber, this.name});

    Grievance.fromJson(Map<String, dynamic> json) {
        this.serialNumber = json["serial_number"];
        this.name = json["name"];
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data["serial_number"] = this.serialNumber;
        data["name"] = this.name;
        return data;
    }
}