Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将具有date属性的json数据转换为dart_Json_Flutter_Dart - Fatal编程技术网

如何将具有date属性的json数据转换为dart

如何将具有date属性的json数据转换为dart,json,flutter,dart,Json,Flutter,Dart,我使用了一个转换器,但结果是错误的… 我正在尝试使用颤振开发有关新冠病毒-19的应用程序,我有点被这种结果格式所困扰。我需要转换为dart以继续使用该应用程序,因此任何帮助都将受到感谢:) 这是api请求的结果: 这是转换器的结果: 历史类{ 弦国; 空省; 时间线; 历史的({这个国家,这个省,这个时间线}); fromJson(映射json){ country=json['country']; 省=json['province']; timeline=json['timeline']!=nu

我使用了一个转换器,但结果是错误的…
我正在尝试使用颤振开发有关新冠病毒-19的应用程序,我有点被这种结果格式所困扰。我需要转换为dart以继续使用该应用程序,因此任何帮助都将受到感谢:)

这是api请求的结果: 这是转换器的结果:
历史类{
弦国;
空省;
时间线;
历史的({这个国家,这个省,这个时间线});
fromJson(映射json){
country=json['country'];
省=json['province'];
timeline=json['timeline']!=null
?新的Timeline.fromJson(json['Timeline'])
:null;
}
映射到JSON(){
最终地图数据=新地图();
数据['country']=这个国家;
数据['province']=本省;
如果(this.timeline!=null){
data['timeline']=this.timeline.toJson();
}
返回数据;
}
}
班级时间表{
例;
例死亡;
2例痊愈;
时间线({this.cases,this.death,this.recovered});
Timeline.fromJson(映射json){
cases=json['cases']!=null?新案例。fromJson(json['cases']):null;
Deathers=json['Deathers']!=null?新案例。fromJson(json['Deathers']):null;
recovered=json['recovered']!=null
?新的Cases.fromJson(json['recovered'])
:null;
}
映射到JSON(){
最终地图数据=新地图();
if(this.cases!=null){
data['cases']=this.cases.toJson();
}
if(this.death!=null){
data['death']=this.death.toJson();
}
如果(this.recovered!=null){
data['recovered']=this.recovered.toJson();
}
返回数据;
}
}
类别案例{
int i31620;
int i31720;
int i31820;
int i31920;
int i32020;
int i32120;
int i32220;
int i32320;
int i32420;
int i32520;
int i32620;
int i32720;
int i32820;
int i32920;
int i33020;
int i33120;
int i4120;
int i4220;
int i4320;
int i4420;
int i4520;
int i4620;
int i4720;
int i4820;
int i4920;
int i41020;
int i41120;
int i41220;
int i41320;
int i41420;
案例(
{这是i31620,
这是i31720,
这是i31820,
这是i31920,
这是i32020,
这是i32120,
这是i32220,
这是i32320,
这是i32420,
这是i32520,
这是i32620,
这是i32720,
这是i32820,
这是i32920,
这是i33020,
这是i33120,
这是i4120,
这是i4220,
这是i4320,
这是i4420,
这是i4520,
这是i4620,
这是i4720,
这是i4820,
这是i4920,
这是i41020,
这是i41120,
这是i41220,
这是i41320,
这是(i41420});
Cases.fromJson(映射json){
i31620=json['3/16/20'];
i31720=json['3/17/20'];
i31820=json['3/18/20'];
i31920=json['3/19/20'];
i32020=json['3/20/20'];
i32120=json['3/21/20'];
i32220=json['3/22/20'];
i32320=json['3/23/20'];
i32420=json['3/24/20'];
i32520=json['3/25/20'];
i32620=json['3/26/20'];
i32720=json['3/27/20'];
i32820=json['3/28/20'];
i32920=json['3/29/20'];
i33020=json['3/30/20'];
i33120=json['3/31/20'];
i4120=json['4/1/20'];
i4220=json['4/2/20'];
i4320=json['4/3/20'];
i4420=json['4/4/20'];
i4520=json['4/5/20'];
i4620=json['4/6/20'];
i4720=json['4/7/20'];
i4820=json['4/8/20'];
i4920=json['4/9/20'];
i41020=json['4/10/20'];
i41120=json['4/11/20'];
i41220=json['4/12/20'];
i41320=json['4/13/20'];
i41420=json['4/14/20'];
}
映射到JSON(){
最终地图数据=新地图();
数据['3/16/20']=this.i31620;
数据['3/17/20']=this.i31720;
数据['3/18/20']=this.i31820;
数据['3/19/20']=this.i31920;
数据['3/20/20']=this.i32020;
数据['3/21/20']=this.i32120;
数据['3/22/20']=this.i32220;
数据['3/23/20']=this.i32320;
数据['3/24/20']=this.i32420;
数据['3/25/20']=this.i32520;
数据['3/26/20']=this.i32620;
数据['3/27/20']=this.i32720;
数据['3/28/20']=this.i32820;
数据['3/29/20']=this.i32920;
数据['3/30/20']=this.i33020;
数据['3/31/20']=this.i33120;
数据['4/1/20']=this.i4120;
数据['4/2/20']=this.i4220;
数据['4/3/20']=this.i4320;
数据['4/4/20']=this.i4420;
数据['4/5/20']=this.i4520;
数据['4/6/20']=this.i4620;
数据['4/7/20']=this.i4720;
数据['4/8/20']=this.i4820;
数据['4/9/20']=this.i4920;
数据['4/10/20']=this.i41020;
数据['4/11/20']=this.i41120;
数据['4/12/20']=this.i41220;
数据['4/13/20']=this.i41320;
数据['4/14/20']=this.i41420;
返回数据;
}
}

您可以使用这个简单的函数将日期字符串转换为
DateTime
对象

DateTime parseDate(String raw){
    int year = int.parse(raw[2]);
    int month = int.parse(raw[0]);
    int day = int.parse(raw[1]);
    return DateTime(year, month, day);
}

您可以使用这个简单的函数将日期字符串转换为
DateTime
对象

DateTime parseDate(String raw){
    int year = int.parse(raw[2]);
    int month = int.parse(raw[0]);
    int day = int.parse(raw[1]);
    return DateTime(year, month, day);
}
更新我找到的解决方案:
导入'dart:convert';
List historicFromJson(字符串str)=>
List.from(json.decode(str).map((x)=>Historic.fromJson(x));
字符串historicToJson(列表数据)=>
encode(List.from(data.map((x)=>x.toJson());
历史类{
弦国;
弦省;
时间线;
历史的({
这个国家,
这个省,,
这个时间线,
});
工厂历史。fromJson(映射json)=>历史(
国家:json[“国家”],
省:json[“省”]==null?null:json[“省”],
timeline:timeline.fromJson(json[“timeline”]),
);
映射到JSON()=>{
“国家”:国家,
“省”:省==空?空:省,
“timeline”:timeline.toJson(),
};
}
班级时间表{
地图案例;
地图死亡;
地图恢复;
时间线({
这种情况,
这就是死亡,
这个,恢复了,,
});
factory Timeline.fromJson(映射json)=>Timeline(
案例:
Map.from(json[“cases”]).Map((k,v)=>MapEntry(
DateTime parseDate(String raw){
    int year = int.parse(raw[2]);
    int month = int.parse(raw[0]);
    int day = int.parse(raw[1]);
    return DateTime(year, month, day);
}
import 'dart:convert';

List<Historic> historicFromJson(String str) =>
    List<Historic>.from(json.decode(str).map((x) => Historic.fromJson(x)));

String historicToJson(List<Historic> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Historic {
  String country;
  String province;
  Timeline timeline;

  Historic({
    this.country,
    this.province,
    this.timeline,
  });

  factory Historic.fromJson(Map<String, dynamic> json) => Historic(
        country: json["country"],
        province: json["province"] == null ? null : json["province"],
        timeline: Timeline.fromJson(json["timeline"]),
      );

  Map<String, dynamic> toJson() => {
        "country": country,
        "province": province == null ? null : province,
        "timeline": timeline.toJson(),
      };
}

class Timeline {
  Map<String, int> cases;
  Map<String, int> deaths;
  Map<String, int> recovered;

  Timeline({
    this.cases,
    this.deaths,
    this.recovered,
  });

  factory Timeline.fromJson(Map<String, dynamic> json) => Timeline(
        cases:
            Map.from(json["cases"]).map((k, v) => MapEntry<String, int>(k, v)),
        deaths:
            Map.from(json["deaths"]).map((k, v) => MapEntry<String, int>(k, v)),
        recovered: Map.from(json["recovered"])
            .map((k, v) => MapEntry<String, int>(k, v)),
      );

  Map<String, dynamic> toJson() => {
        "cases": Map.from(cases).map((k, v) => MapEntry<String, dynamic>(k, v)),
        "deaths":
            Map.from(deaths).map((k, v) => MapEntry<String, dynamic>(k, v)),
        "recovered":
            Map.from(recovered).map((k, v) => MapEntry<String, dynamic>(k, v)),
      };
}