如何将这个复杂的Json转换为Dart对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将这个复杂的Json转换为Dart对象相关的知识,希望对你有一定的参考价值。

我有这个复杂的(对我来说)json对象,我想将其转换为dart模型,但我总是遇到一个错误或另一个错误。

这是这样的json对象的列表:


    "type": "FeatureCollection",
    "features": [
        
            "type": "Feature",
            "properties": 
                "name": "Congo",
                "pop_est": 4012809,
                "gdp_md_est": 15350,
                "economy": "6. Developing region",
                "income_grp": "4. Lower middle income",
                "iso_a2": "CG",
                "iso_a3": "COG",
                "continent": "Africa",
                "latitude": "-4.2634",
                "longitude": "15.2832",
                "confirmed": 143,
                "deaths": 6,
                "active": 126,
                "recovered": 11
            ,
            "geometry": 
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            12.995517205465177,
                            -4.781103203961884
                        ],
                        [
                            12.620759718484491,
                            -4.438023369976136
                        ],
                        [
                            12.318607618873926,
                            -4.606230157086188
                        ],
                        [
                            11.91496300624209,
                            -5.037986748884791
                        ],
                        [
                            11.093772820691925,
                            -3.978826592630547
                        ],
                        [
                            11.855121697648114,
                            -3.426870619321051
                        ],
                        [
                            11.478038771214303,
                            -2.765618991714241
                        ],
                        [
                            11.820963575903193,
                            -2.514161472181982
                        ],
                        [
                            12.495702752338161,
                            -2.391688327650243
                        ],
                        [
                            12.575284458067642,
                            -1.948511244315135
                        ],
                        [
                            13.109618767965628,
                            -2.428740329603514
                        ],
                        [
                            13.99240726080771,
                            -2.4708049454891
                        ],
                        [
                            14.299210239324564,
                            -1.998275648612214
                        ],
                        [
                            14.425455763413593,
                            -1.333406670744971
                        ],
                        [
                            14.316418491277743,
                            -0.552627455247048
                        ],
                        [
                            13.843320753645655,
                            0.038757635901149
                        ],
                        [
                            14.276265903386957,
                            1.196929836426619
                        ],
                        [
                            14.026668735417218,
                            1.395677395021153
                        ],
                        [
                            13.282631463278818,
                            1.31418366129688
                        ],
                        [
                            13.003113641012078,
                            1.83089630778332
                        ],
                        [
                            13.075822381246752,
                            2.267097072759015
                        ],
                        [
                            14.33781253424658,
                            2.227874660649491
                        ],
                        [
                            15.146341993885244,
                            1.964014797367184
                        ],
                        [
                            15.940918816805064,
                            1.727672634280295
                        ],
                        [
                            16.012852410555354,
                            2.267639675298085
                        ],
                        [
                            16.537058139724135,
                            3.198254706226279
                        ],
                        [
                            17.133042433346304,
                            3.728196519379452
                        ],
                        [
                            17.809900343505262,
                            3.56019643799857
                        ],
                        [
                            18.45306521980993,
                            3.504385891123349
                        ],
                        [
                            18.39379235197114,
                            2.90044342692822
                        ],
                        [
                            18.094275750407434,
                            2.365721543788055
                        ],
                        [
                            17.898835483479587,
                            1.741831976728278
                        ],
                        [
                            17.774191928791566,
                            0.855658677571085
                        ],
                        [
                            17.826540154703252,
                            0.288923244626105
                        ],
                        [
                            17.66355268725468,
                            -0.058083998213817
                        ],
                        [
                            17.638644646889986,
                            -0.424831638189247
                        ],
                        [
                            17.523716261472856,
                            -0.743830254726987
                        ],
                        [
                            16.865306837642123,
                            -1.225816338713287
                        ],
                        [
                            16.407091912510054,
                            -1.740927015798682
                        ],
                        [
                            15.972803175529151,
                            -2.712392266453612
                        ],
                        [
                            16.0062895036543,
                            -3.535132744972529
                        ],
                        [
                            15.75354007331475,
                            -3.855164890156096
                        ],
                        [
                            15.170991652088444,
                            -4.343507175314301
                        ],
                        [
                            14.58260379401318,
                            -4.97023894615014
                        ],
                        [
                            14.209034864975223,
                            -4.793092136253598
                        ],
                        [
                            14.144956088933299,
                            -4.510008640158715
                        ],
                        [
                            13.600234816144678,
                            -4.50013844159097
                        ],
                        [
                            13.258240187237048,
                            -4.882957452009165
                        ],
                        [
                            12.995517205465177,
                            -4.781103203961884
                        ]
                    ]
                ]
            
        ,
        
            "type": "Feature",
            "properties": 
                "name": "Angola",
                "pop_est": 12799293,
                "gdp_md_est": 110300,
                "economy": "7. Least developed region",
                "income_grp": "3. Upper middle income",
                "iso_a2": "AO",
                "iso_a3": "AGO",
                "continent": "Africa",
                "latitude": "-11.2027",
                "longitude": "17.8739",
                "confirmed": 24,
                "deaths": 2,
                "active": 16,
                "recovered": 6
            ,
            "geometry": 
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                16.326528354567046,
                                -5.877470391466218
                            ],
                            [
                                16.57317996589614,
                                -6.622644545115094
                            ],
                            [
                                16.86019087084523,
                                -7.222297865429979
                            ],
                            [
                                17.08999596524717,
                                -7.545688978712476
                            ],
                            [
                                17.472970004962292,
                                -8.068551120641656
                            ],
                            [
                                18.13422163256905,
                                -7.987677504104866
                            ],
                            [
                                18.464175652752687,
                                -7.847014255406477
                            ],
                            [
                                19.01675174324967,
                                -7.98824594486014
                            ],
                            [
                                19.166613396896082,
                                -7.738183688999725
                            ],
                            [
                                19.417502475673217,
                                -7.155428562044278
                            ],
                            [
                                20.037723016040218,
                                -7.11636117923166
                            ],
                            [
                                20.09162153492062,
                                -6.943090101756951
                            ],
                            [
                                20.601822950938327,
                                -6.939317722199689
                            ],
                            [
                                20.51474816252653,
                                -7.299605808138665
                            ],
                            [
                                21.728110792739756,
                                -7.290872491081316
                            ],
                            [
                                21.746455926203367,
                                -7.920084730667114
                            ],
                            [
                                21.94913089365204,
                                -8.305900974158305
                            ],
                            [
                                21.801801385187957,
                                -8.908706556842986
                            ],
                            [
                                21.875181919042404,
                                -9.523707777548566
                            ],
                            [
                                22.208753289486424,
                                -9.89479623783653
                            ],
                            [
                                22.155268182064333,
                                -11.084801120653779
                            ],
                            [
                                22.402798292742432,
                                -10.99307545333569
                            ],
                            [
                                22.83734541188477,
                                -11.017621758674338
                            ],
                            [
                                23.456790805767465,
                                -10.867863457892483
                            ],
                            [
                                23.912215203555746,
                                -10.926826267137542
                            ],
                            [
                                24.017893507592614,
                                -11.237298272347118
                            ],
                            [
                                23.90415368011824,
                                -11.722281589406336
                            ],
                            [
                                24.079905226342902,
                                -12.191296888887308
                            ],
                            [
                                23.930922072045377,
                                -12.565847670138822
                            ],
                            [
                                24.016136508894704,
                                -12.911046237848552
                            ],
                            [
                                21.933886346125945,
                                -12.898437188369357
                            ],
                            [
                                21.887842644953878,
                                -16.080310153876894
                            ],
                            [
                                22.56247846852429,
                                -16.898451429921835
                            ],
                            [
                                23.215048455506093,
                                -17.523116143465952
                            ],
                            [
                                21.377176141045595,
                                -17.93063648851971
                            ],
                            [
                                18.95618696460363,
                                -17.789094740472237
                            ],
                            [
                                18.26330936043422,
                                -17.309950860262006
                            ],
                            [
                                14.209706658595051,
                                -17.353100681225712
                            ],
                            [
                                14.058501417709039,
                                -17.423380629142656
                            ],
                            [
                                13.462362094789967,
                                -16.97121184658874
                            ],
                            [
                                12.814081251688407,
                                -16.941342868724078
                            ],
                            [
                                12.215461460019384,
                                -17.111668389558062
                            ],
                            [
                                11.734198846085148,
                                -17.3018893368245
                            ],
                            [
                                11.64009606288161,
                                -16.67314218512921
                            ],
                            [
                                11.778537224991567,
                                -15.79381601325069
                            ],
                            [
                                12.123580763404448,
                                -14.878316338767931
                            ],
                            [
                                12.175618930722266,
                                -14.449143568583892
                            ],
                            [
                                12.500095249083017,
                                -13.547699883684402
                            ],
                            [
                                12.738478631245442,
                                -13.137905775609935
                            ],
                            [
                                13.312913852601838,
                                -12.483630466362513
                            ],
                            [
                                13.633721144269828,
                                -12.038644707897191
                            ],
                            [
                                13.738727654686926,
                                -11.297863050993143
                            ],
                            [
                                13.686379428775297,
                                -10.731075941615842
                            ],
                            [
                                13.38732791510216,
                                -10.373578383020728
                            ],
                            [
                                13.120987583069875,
                                -9.766897067914115
                            ],
                            [
                                12.875369500386569,
                                -9.166933689005488
                            ],
                            [
                                12.929061313537801,
                                -8.959091078327575
                            ],
                            [
                                13.236432732809874,
                                -8.562629489784342
                            ],
                            [
                                12.933040398824316,
                                -7.596538588087753
                            ],
                            [
                                12.72829837408392,
                                -6.927122084178805
                            ],
                            [
                                12.227347039446443,
                                -6.294447523629372
                            ],
                            [
                                12.322431674863566,
                                -6.100092461779653
                            ],
                            [
                                12.735171339578699,
                                -5.965682061388478
                            ],
                            [
                                13.02486941900699,
                                -5.984388929878108
                            ],
                            [
                                13.375597364971895,
                                -5.864241224799557
                            ],
                            [
                                16.326528354567046,
                                -5.877470391466218
                            ]
                        ]
                    ],
                    [
                        [
                            [
                                12.436688266660923,
                                -5.684303887559224
                            ],
                            [
                                12.18233686692028,
                                -5.789930515163803
                            ],
                            [
                                11.914963006242116,
                                -5.037986748884734
                            ],
                            [
                                12.318607618873926,
                                -4.606230157086158
                            ],
                            [
                                12.62075971848455,
                                -4.438023369976121
                            ],
                            [
                                12.995517205465205,
                                -4.781103203961919
                            ],
                            [
                                12.631611769265845,
                                -4.991271254092936
                            ],
                            [
                                12.468004184629763,
                                -5.248361504744992
                            ],
                            [
                                12.436688266660923,
                                -5.684303887559224
                            ]
                        ]
                    ]
                ]
            
        
    ] //Missing bracket

但是我只需要这部分:

"properties": 
                    "name": "Angola",
                    "pop_est": 12799293,
                    "gdp_md_est": 110300,
                    "economy": "7. Least developed region",
                    "income_grp": "3. Upper middle income",
                    "iso_a2": "AO",
                    "iso_a3": "AGO",
                    "continent": "Africa",
                    "latitude": "-11.2027",
                    "longitude": "17.8739",
                    "confirmed": 24,
                    "deaths": 2,
                    "active": 16,
                    "recovered": 6
                ,

我尝试为上面需要的零件创建模型,如下所示:

class AfricaData 
  Properties properties;

  AfricaData(this.properties);

  AfricaData.fromJson(Map<String, dynamic> json) 
    properties = json['properties'] != null
        ? new Properties.fromJson(json['properties'])
        : null;
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.properties != null) 
      data['properties'] = this.properties.toJson();
    
    return data;
  


class Properties 
  String name;
  int popEst;
  int gdpMdEst;
  String economy;
  String incomeGrp;
  String isoA2;
  String isoA3;
  String continent;
  String latitude;
  String longitude;
  int confirmed;
  int deaths;
  int active;
  int recovered;

  Properties(
      this.name,
      this.popEst,
      this.gdpMdEst,
      this.economy,
      this.incomeGrp,
      this.isoA2,
      this.isoA3,
      this.continent,
      this.latitude,
      this.longitude,
      this.confirmed,
      this.deaths,
      this.active,
      this.recovered);

  Properties.fromJson(Map<String, dynamic> json) 
    name = json['name'];
    popEst = json['pop_est'];
    gdpMdEst = json['gdp_md_est'];
    economy = json['economy'];
    incomeGrp = json['income_grp'];
    isoA2 = json['iso_a2'];
    isoA3 = json['iso_a3'];
    continent = json['continent'];
    latitude = json['latitude'];
    longitude = json['longitude'];
    confirmed = json['confirmed'];
    deaths = json['deaths'];
    active = json['active'];
    recovered = json['recovered'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['pop_est'] = this.popEst;
    data['gdp_md_est'] = this.gdpMdEst;
    data['economy'] = this.economy;
    data['income_grp'] = this.incomeGrp;
    data['iso_a2'] = this.isoA2;
    data['iso_a3'] = this.isoA3;
    data['continent'] = this.continent;
    data['latitude'] = this.latitude;
    data['longitude'] = this.longitude;
    data['confirmed'] = this.confirmed;
    data['deaths'] = this.deaths;
    data['active'] = this.active;
    data['recovered'] = this.recovered;
    return data;
  

到目前为止,我已经尝试了以下方法:

1)

  var data = json.decode(response.body);

          var dataModel = (data[1]['features'][0]['properties'] as List).map((f) => AfricaData.fromJson(f)).toList();

2)

Map<String, dynamic> data = json.decode(response.body);

      var dataModel = (data['type']['features']['type']['properties'] as List).map((f) => AfricaData.fromJson(f)).toList();

但是我一直得到不同的错误,特别是InternalLinkedHashMap'不是强制类型转换中'List'类型的子类型>]

我有这个复杂的(对我来说)json对象,我想将其转换为dart模型,但我不断遇到一个错误或另一个错误。它是这样的json对象的列表:“ type”:“ FeatureCollection”,...

答案

为了帮助您,您可以使用从json到Dart的转换器

以上是关于如何将这个复杂的Json转换为Dart对象的主要内容,如果未能解决你的问题,请参考以下文章

如何正确使用javascript反序列化将json字符串转换为复杂对象?

6-4 JSON解析与复杂模型转换实用技巧

php如何将json对象转字符串

让复杂Json数据和对象自由转换 --- Gson

将对象转换为可编码对象失败

JAVA中如何将一个json形式的字符串转为json对象?