9Cells

JSON

예제

List 타입의 모델 생성 & 모델 하위로 모델 생성
import 'question.dart';
import 'answer.dart';

class Exam {
  final int id;
  final Question question;
  final List<Answer> answers;

  Exam({this.id, this.question, this.answers});

  factory Exam.fromJson(Map<String, dynamic> json) {
    List dataAnswers = json['answers'];
    List<Answer> answers = null;
    if (dataAnswers != null) {
      answers = dataAnswers.map((data) => Answer.fromJson(data)).toList();
    }
    return Exam(
      id: json['id'],
      question: json['question'] != null ? Question.fromJson(json['question']) : null,
      answers: answers,
    );
  }
}