복잡한 JSON 가져오기

다은·2021년 5월 6일
0

🎯 Flutter

목록 보기
13/27
class Baby {
  final String babycode;
  final String name;
  final String gender;
  final String birth;
  final String occur;
  final int age;
  final int severity;
  final List<Item> heredity;
  final String relation;
  final String fosterType;
  final String membercode;
  final List<Item> symptom;
  final List<Item> disease;
  final List<Item> avoidFood;
  final List<Item> avoidEtc;
  final List<Item> front;
  final List<Item> back;
  final String profileUrl;

  Baby({this.name, this.gender, this.birth, this.occur, this.age, this.severity, this.heredity, this.relation, this.fosterType, this.membercode, this.symptom, this.disease, this.avoidFood, this.avoidEtc, this.front, this.back, this.profileUrl, this.babycode});

  factory Baby.fromJson(Map<String, dynamic> json) {
    List<Item> familyList = List<Item>();
    List<Item> symptomList = List<Item>();
    List<Item> diseaseList = List<Item>();
    List<Item> allergenFoodList = List<Item>();
    List<Item> allergenEtcList = List<Item>();
    List<Item> frontList = List<Item>();
    List<Item> backList = List<Item>();

    if(json['heredity']!=null) {
      for (int i = 0; i < json['heredity']['family'].length; i++) {
        Item v = new Item(
          name: json['heredity']['family'][i]['name'].toString(),
        );
        familyList.add(v);
      }
    }

    if(json['symptomType']!=null) {
      for (int i = 0; i < json['symptomType']['symptom'].length; i++) {
        Item v = new Item(
          name: json['symptomType']['symptom'][i]['name'].toString(),
        );
        symptomList.add(v);
      }
    }

    if(json['diseaseType']!=null) {
      for (int i = 0; i < json['diseaseType']['disease'].length; i++) {
        Item v = new Item(
          name: json['diseaseType']['disease'][i]['name'].toString(),
        );
        diseaseList.add(v);
      }
    }

    if(json['allergenFood']!=null) {
      for (int i = 0; i < json['allergenFood']['food'].length; i++) {
        Item v = new Item(
          name: json['allergenFood']['food'][i]['name'].toString(),
        );
        allergenFoodList.add(v);
      }
    }

    if(json['allergenMatter']!=null) {
      for (int i = 0; i < json['allergenMatter']['matter'].length; i++) {
        Item v = new Item(
          name: json['allergenMatter']['matter'][i]['name'].toString(),
        );
        allergenEtcList.add(v);
      }
    }

    if(json['regionBack']!=null) {
      for (int i = 0; i < json['regionBack']['back'].length; i++) {
        Item v = new Item(
          name: json['regionBack']['back'][i]['name'].toString(),
        );
        backList.add(v);
      }
    }

    if(json['regionFront']!=null) {
      for (int i = 0; i < json['regionFront']['front'].length; i++) {
        Item v = new Item(
          name: json['regionFront']['front'][i]['name'].toString(),
        );
        frontList.add(v);
      }
    }

    return Baby(
      babycode: json['babyCode'] as String,
      name: json['name'] as String,
      gender: json['gender'] as String,
      birth: json['birth'] as String,
      occur: json['occur'] as String,
      relation: json['relation'] as String,
      fosterType: json['fosterType'] as String,
      age: json['age'] as int,
      severity: json['severity'] as int,
      heredity: json['heredity']!=null?familyList:null,
      symptom: json['symptomList']!=null?symptomList:null,
      disease: json['diseaseType']!=null?diseaseList:null,
      avoidFood: json['avoidFoodList']!=null?allergenFoodList:null,
      avoidEtc: json['avoidEtcList']!=null?allergenEtcList:null,
      front:json['regionFront']!=null?frontList:null,
      back: json['regionBack']!=null?backList:null,
      profileUrl: json['profileUrl'] as String == null ? "" : json['profileUrl'] as String
    );
  }
}

{
  "babyCode": "B0000023",
  "name": "우민",
  "gender": "M",
  "birth": "2015-09-07",
  "occur": "2016-05-31",
  "heredity": {
    "family": [
      "아빠",
      "친가"
    ]
  },
  "profileUrl": null,
  "regDt": null,
  "mdfyDt": null,
  "dropDt": null,
  "age": 5,
  "heredityList": null,
  "relation": null,
  "fosterType": null,
  "memberCode": null,
  "symptomList": null,
  "severity": 9,
  "avoidFoodList": null,
  "avoidEtcList": null,
  "partFrontList": null,
  "partBackList": null,
  "diseaseList": null,
  "symptomType": {
    "symptom": [
      "긁은상처",
      "건조"
    ]
  },
  "regionFront": {
    "front": [
      "왼쪽 무릎",
      "오른쪽 무릎"
    ]
  },
  "regionBack": {
    "back": [
      "윗등",
      "허리"
    ]
  },
  "allergenFood": {
    "food": [
      {
        "name": "계란흰자",
        "code": 1
      },
      {
        "name": "계란노른자",
        "code": 2
      },
      {
        "name": "닭고기",
        "code": 4
      },
      {
        "name": "대구",
        "code": 12
      },
      {
        "name": "게",
        "code": 13
      },
      {
        "name": "사과",
        "code": 18
      }
    ]
  },
  "allergenMatter": {
    "matter": [
      "고양이털",
      "강아지털",
      "바퀴벌레"
    ]
  },
  "diseaseType": null
}
profile
천방지축 어리둥절 빙글빙글

0개의 댓글