flutter Dart -如何搜索JSON?

7lrncoxx  于 2022-12-05  发布在  Flutter
关注(0)|答案(3)|浏览(154)

我有一个如下的JSON代码:

final String response = await rootBundle.loadString('assets/Schools.json');
List data = await json.decode(response);
print(data);

/* output:
[{ "city": "ISTANBUL", "districty": "Kagithane", "name": "Kagithane anadolu lisesi"}, { "city": "ISTANBUL", "districty": "Sisli", "name": "Aziz Sancar anadolu lisesi"}, { "city": "IZMIR", "districty": "Adalar", "name": "Kemal Sunal anadolu lisesi"}, { "city": "ISTANBUL", "districty": "Bagcilar", "name": "Bagcilar Fen lisesi"}, { "city": "ISTANBUL", "districty": "Kagithane", "name": "Kagithane Meslek lisesi"}]
*/

AI还写了这样一个模型:

List <School> schools = [];

List<School> allSchools() {
  return schools;
}

class School {
  String city;
  String districty;
  String name;
  School({required this.city, required this.name, required this.districty});
}

如何将JSON中的数据导出到list中?所以我想这样传递它:

List <School> schools = [
  School(city: "ISTANBUL", districty: "Kagithane", name: "Kagithane anadolu lisesi"),
  School(city: "ISTANBUL", districty: "Sisli", name: "Aziz Sancar anadolu lisesi"),
  School(city: "IZMIR", districty: "ADALAR", name: "Kemal Sunal anadolu lisesi"),
  School(city: "ISTANBUL", districty: "BAGCILAR", name: "Bagcilar Fen lisesi"),
  School(city: "ISTANBUL", districty: "Kagithane", name: "Kagithane Meslek lisesi")
];

我提前感谢你的帮助,谢谢。

oiopk7p5

oiopk7p51#

正如@Spanching所说,工厂使用工厂是最好的方法,因为它使用起来简单高效。
但是,由于您的问题包含如何搜索特定的学校,我将在这里添加一些额外的提示。
首先,创建一个工厂构造函数:

class School {
  String city;
  String districty;
  String name;
  School({required this.city, required this.name, required this.districty});

  factory School.fromJson(Map<String, dynamic> json) {
    return School(
      city: json['city'],
      name: json['name'],
      districty: json['district'],
    );
  }
}

现在创建另一个类School以轻松处理数据:

class Schools {
  List<School> list;
  Schools({required this.list});

  factory Schools.fromJson(List<dynamic> json) {
    return Schools(
      list: json.map((e) => School.fromJson(e)).toList(),
    );
  }
}

加入getter以取得类别的执行严修:

static Future<Schools> get instance async {
    final String response = await rootBundle.loadString('assets/Schools.json');
    List data = await json.decode(response);
    return Schools.fromJson(data);
  }

根据需要添加方法。例如,如果要按城市搜索学校,可以添加如下方法:

List<School> searchByCity(String city) {
    return list.where((element) => element.city == city).toList();
  }

现在您的主程序甚至可以像这样可读:

final schools = await Schools.instance;
  final schoolList = schools.searchByCity('ISTANBUL');
  print(schoolList);

这段代码很长,但我希望它能对你有所帮助。如果你想看一下整个代码,你可以check it here

ovfsdjhp

ovfsdjhp2#

可以使用List.map()方法并传递一个函数,该函数从列表中的每个JSON对象创建一个School对象:

List data = json.decode(response);
schools = data.map((schoolData) => School(
  city: schoolData['city'],
  districty: schoolData['districty'],
  name: schoolData['name']
)).toList();
5m1hhzi4

5m1hhzi43#

您可以在学校类中使用工厂,如下所示:

factory School.fromJson(Map<String, dynamic> json) {
  return School(city: json["city"], districty: json["districty"], name: json["name"]);
}

然后从您的json:

var schools = data.map((school) => School.fromJson(school)).toList();

相关问题