【问题标题】:How to fetch data from JSON that contain arrays of arrays into object in flutter如何从包含数组数组的JSON中获取数据到颤动的对象中
【发布时间】:2022-10-22 19:18:31
【问题描述】:

我得到的 json 看起来像这样:

{"-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"}]}

“DayList”中的每个列表代表单独的工作日。例如。 ["7.50","12.00"] 只是说,工作时间从 7.30 开始,到 12 结束。每天的索引也代表一个月中的一天。索引 0 是 01/06/2022,索引 1 是 02/06/2022,依此类推。整个列表附加到一个人,在本例中为 John Smith。

我的 Day 模型如下所示:

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

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

任何人都知道如何基于这个 json 创建对象列表? 也许有更好的方法来创建这样的 json?

【问题讨论】:

标签: json flutter dart backend


【解决方案1】:
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,
};
}

您可以使用此模型来解析每个对象的数据

【讨论】:

    【解决方案2】:
    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,
          };
    }
    

    您也可以使用this 网站直接创建模型

    【讨论】:

      【解决方案3】:
      import 'dart:convert';
      import 'package:http/http.dart' as http;
      
      class Album {
        final List<Item> list;
        const Album({required this.list});
      }
      
      class Item {
        final int userId;
        final int id;
        final String title;
        const Item({required this.userId, required this.id, required this.title});
        factory Item.fromJson(Map<String, dynamic> json) =>
            Item(userId: json['userId'], id: json['id'], title: json['title']);
      }
      
      Future<Album> fetchAlbum() async {
        List<Item> result = [];
        final response = await http
            .get(Uri.parse('https://jsonplaceholder.typicode.com/albums?_limit=3'));
        if (response.statusCode == 200) {
          for (var element in jsonDecode(response.body)) {
            result.add(Item.fromJson(element));
          }
          return Album(list: result);
        } else {
          throw Exception('Opps, something went wrong!');
        }
      }
      
      void main(List<String> args) {
        fetchAlbum().then((value) => print(value.list[0].title));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-07
        • 2022-01-22
        • 2019-09-17
        相关资源
        最近更新 更多