Home > Blockchain >  How to fetch data from JSON that contain arrays of arrays into object in flutter
How to fetch data from JSON that contain arrays of arrays into object in flutter

Time:06-09

I am getting json that looks like this:

{"-N42h_BKjWaEZRJyH8vS":[{"Data":"06/2022","imie":"John","DayList":[["7.50","12.00"],["00.00","00.00"],["8.00","16.00"],["00.00","00.00"],["00.00","00.00"],["7.50","15.00"],["00.00","00.00"],["00.00","00.00"],["00.00","00.00"],["8.00","15.25"],["00.00","00.00"],["00.00","00.00"],["7.50","15.00"],["00.00","00.00"]],"surname":"Smith"}]}

Each List inside "DayList" represents separate working day. E.g. ["7.50","12.00"] is simply saying that, work time starts at 7.30 and end at 12. Each day index also represents day of month E.g. index 0 is 01/06/2022, index 1 is 02/06/2022 and so on. The whole List is attached to one person, in this case John Smith.

My Day model looks like this:

class Day {
  String name;
  DateTime dateTime;
  double startTime;
  double endTime;

  Day({
    required this.name,
    required this.dateTime,
    required this.startTime,
    required this.endTime,
  });
}

Anyone have an idea how to create List of objects based on this json? Maybe there is better way of creating such a json?

CodePudding user response:

class DayDatum {
DayDatum({
    this.data,
    this.imie,
    this.dayList,
    this.surname,
});

String data;
String imie;
List<List<String>> dayList;
String surname;

factory DayDatum.fromJson(Map<String, dynamic> json) => DayDatum(
    data: json["Data"],
    imie: json["imie"],
    dayList: List<List<String>>.from(json["DayList"].map((x) => List<String>.from(x.map((x) => x)))),
    surname: json["surname"],
);

Map<String, dynamic> toJson() => {
    "Data": data,
    "imie": imie,
    "DayList": List<dynamic>.from(dayList.map((x) => List<dynamic>.from(x.map((x) => x)))),
    "surname": surname,
};
}

You can use this model to parse data of each object

CodePudding user response:

Please check below link for the json parser. https://docs.flutter.dev/development/data-and-backend/json#setting-up-json_serializable-in-a-project

If you have any query there, please let me know.

CodePudding user response:

import 'dart:convert';

Data dataFromJson(String str) => Data.fromJson(json.decode(str));

String dataToJson(Data data) => json.encode(data.toJson());

class Data {
  Data({
    this.listOfDays,
  });

  List<personalInfo>? listOfDays;

  factory Data.fromJson(Map<String, dynamic> json) => Data(
        listOfDays: List<personalInfo>.from(json["-N42h_BKjWaEZRJyH8vS"]
            .map((x) => personalInfo.fromJson(x))),
      );

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

class personalInfo {
  personalInfo({
    this.data,
    this.imie,
    this.dayList,
    this.surname,
  });

  String? data;
  String? imie;
  List<List<String>>? dayList;
  String? surname;

  factory personalInfo.fromJson(Map<String, dynamic> json) =>
      personalInfo(
        data: json["Data"],
        imie: json["imie"],
        dayList: List<List<String>>.from(
            json["DayList"].map((x) => List<String>.from(x.map((x) => x)))),
        surname: json["surname"],
      );

  Map<String, dynamic> toJson() => {
        "Data": data,
        "imie": imie,
        "DayList": List<dynamic>.from(
            dayList!.map((x) => List<dynamic>.from(x.map((x) => x)))),
        "surname": surname,
      };
}

also you can use this site to create models directly

  • Related