扑:我如何从json中获取数据?

时间:2018-11-18 07:23:39

标签: json flutter

我试图通过下面的代码从json中获取数据,但是在此过程中我遇到了错误(类型“列表”不是类型“地图”的子类型)。请帮助我

json结构如下  [{id: 1, fname: Semester Fees, sname: 1st Semester, fee_amount: 4000, status: 0}, {id: 2, fname: Admission Fees, sname: 2nd Semester, fee_amount: 4000, status: 0}]

 String url = 'http://192.168.42.124/my_payment/getcoursefee.php';
  List data;
  Future<String> makeRequest() async {
    var response = await http
        .post(Uri.encodeFull(url), headers: {"Accept": "application/json"},
    body: {
      "student_id": "261",

    });

    setState(() {
      var extractdata = json.decode(response.body);
     Student student =new Student.fromJson(extractdata);
     print(student.studentId);
     print(extractdata);
    });

  }

  @override
  void initState() {
    this.makeRequest();
  }

我的学生班级

class Student{
  String studentId;


  Student({
    this.studentId,
  });

  factory Student.fromJson(Map<String, dynamic> parsedJson){
    return Student(
      studentId: parsedJson['id'],

    );
  }
}

1 个答案:

答案 0 :(得分:1)

List<Map<String, dynamic>> extractdata = json.decode(response.body);
for (map in extractdata) {
  Student student = new Student.fromJson(map);
  print(student.studentId);
  print(map);
}

像这样的Somethimg