나는 flutter를 배우려고 노력하고 있지만 JSON 직렬화에 갇혀 있습니다. YouTube와 flutter 문서에서 몇 가지 튜토리얼을 따르고 있었지만 직렬화에 약간의 어려움을 겪고 있습니다. 조금만 도와 주 시겠어요? 이건 교육 목적이라서 그 자체가 해답 이라기보다는이면의 이론에 더 관심이 있지만 해답만으로도 그 과정을 이해하려고 노력할 수 있다고 생각합니다. 나는 이것을 알고 있다고 언급해야 하지만, 내 경우에는 데이터에 중첩 된 객체가있어 혼란 스럽습니다.
응답 데이터의 샘플은 기본적으로 매일 개점 한 날짜 목록이 있고 각 개체 (요일)에 대한 개점 시간과 날짜가있는 상점입니다.
{
"data": [
{
"openTime": {
"open": "10:00",
"close": "20:00"
},
"date": {
"gregorian": {
"day": "01",
"weekday": {
"en": "Friday",
"de": "Freitag"
},
"month": {
"number": 5,
"en": "May",
"de": "Mai"
},
"year": "2020"
}
}
},
{
"openTime": {
"open": "12:00",
"close": "18:00"
},
"date": {
"gregorian": {
"day": "02",
"weekday": {
"en": "Saturday",
"de": "Samstag"
},
"month": {
"number": 5,
"en": "May",
"de": "Mai"
},
"year": "2020"
}
}
}
]
}
데이터를 가져 오는 내 기능 :
Future<Mall> fetchData() async {
final response = await http.get(url);
if (response.statusCode == 200) {
return Mall.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load data!');
}
}
내 경우 클래스 Mall에는 openTime
및 date
이 있는데, 내 문제가 어디에 있다고 생각합니다.
class Mall {
final dynamic openTime;
final dynamic date;
Mall({this.openTime, this.date});
factory Mall.fromJson(Map<String, dynamic> json) {
return Mall(openTime: json['openTime'], date: json['date']);
}
}
내가 생성하려는 결과는 다음과 같아야 데이터 목록을 반복하고 며칠 동안 카드를 만들 수 있습니다 (예 : 지난 7 일).
var time = [
{
"openTime": {"open": "10:00", "close": "20:00"},
"date": "01 May 2020"
},
{
"openTime": {"open": "12:00", "close": "18:00"},
"date": "02 May 2020"
},
];
내가 얻는 오류는 다음과 같습니다.
type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
더 자세한 정보가 필요하면 알려주세요. 미리 감사드립니다.
코드에 약간의 오류가 있습니다.
1-이 부분에서 응답은 "데이터"라는 컨테이너 개체의 개체입니다.
Future<Mall> fetchData() async {
final response = await http.get(url);
if (response.statusCode == 200) {
return Mall.fromJson(json.decode(response.body)); //here
} else {
throw Exception('Failed to load data!');
}
}
2- 쇼핑몰 데이터는 '데이터'의 중첩 개체이므로 쇼핑몰 클래스를
class Mall {
List<Datum> data;
Mall({
this.data,
});
factory Mall.fromJson(Map<String, dynamic> json) => Mall(
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
OpenTime openTime;
Date date;
Datum({
this.openTime,
this.date,
});
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
openTime: OpenTime.fromJson(json["openTime"]),
date: Date.fromJson(json["date"]),
);
Map<String, dynamic> toJson() => {
"openTime": openTime.toJson(),
"date": date.toJson(),
};
}
class Date {
Gregorian gregorian;
Date({
this.gregorian,
});
factory Date.fromJson(Map<String, dynamic> json) => Date(
gregorian: Gregorian.fromJson(json["gregorian"]),
);
Map<String, dynamic> toJson() => {
"gregorian": gregorian.toJson(),
};
}
class Gregorian {
String day;
Weekday weekday;
Month month;
String year;
Gregorian({
this.day,
this.weekday,
this.month,
this.year,
});
factory Gregorian.fromJson(Map<String, dynamic> json) => Gregorian(
day: json["day"],
weekday: Weekday.fromJson(json["weekday"]),
month: Month.fromJson(json["month"]),
year: json["year"],
);
Map<String, dynamic> toJson() => {
"day": day,
"weekday": weekday.toJson(),
"month": month.toJson(),
"year": year,
};
}
class Month {
int number;
String en;
String de;
Month({
this.number,
this.en,
this.de,
});
factory Month.fromJson(Map<String, dynamic> json) => Month(
number: json["number"],
en: json["en"],
de: json["de"],
);
Map<String, dynamic> toJson() => {
"number": number,
"en": en,
"de": de,
};
}
class Weekday {
String en;
String de;
Weekday({
this.en,
this.de,
});
factory Weekday.fromJson(Map<String, dynamic> json) => Weekday(
en: json["en"],
de: json["de"],
);
Map<String, dynamic> toJson() => {
"en": en,
"de": de,
};
}
class OpenTime {
String open;
String close;
OpenTime({
this.open,
this.close,
});
factory OpenTime.fromJson(Map<String, dynamic> json) => OpenTime(
open: json["open"],
close: json["close"],
);
Map<String, dynamic> toJson() => {
"open": open,
"close": close,
};
}
이제했던 것처럼 쇼핑몰 데이터를 조회 할 수 있습니다.
Future<Mall> fetchData() async {
final response = await http.get(url);
if (response.statusCode == 200) {
return Mall.fromJson(json.decode(response.body)); //here
} else {
throw Exception('Failed to load data!');
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다