Flutter嵌套JSON解析

问题描述 投票:2回答:1

我在这里有一个嵌套的JSON api:

[
  {
    "Employee": {
      "Name": "Michael Jackson",
      "Identification": "881228145031",
      "Company": "Test Corporate",
      "DateOfBirth": "1988-12-28",
      "Entitlements": {
        "GP": {
          "Entitlement": "10000",
          "Utilisation": "500",
          "Balance": "9500"
        },
        "OPS": {
          "Entitlement": "10000",
          "Utilisation": "500",
          "Balance": "9500"
        },
        "IP": {
          "Entitlement": "50000",
          "Utilisation": "17000",
          "Balance": "33000"
        },
        "Dental": {
          "Entitlement": "0.00",
          "Utilisation": "0.00",
          "Balance": "0.00"
        },
        "Optical": {
          "Entitlement": "500",
          "Utilisation": "0.00",
          "Balance": "500"
        },
        "EHS": {
          "Entitlement": "1000",
          "Utilisation": "250",
          "Balance": "750"
        }
      }
    },
    "Dependents": [
      {
        "Name": "Kim",
        "Relationship": "Parent",
        "Entitlements": {
          "GP": {
            "Entitlement": "10000",
            "Utilisation": "500",
            "Balance": "9500"
          },
          "OPS": {
            "Entitlement": "10000",
            "Utilisation": "500",
            "Balance": "9500"
          },
          "IP": {
            "Entitlement": "50000",
            "Utilisation": "17000",
            "Balance": "33000"
          },
          "Dental": {
            "Entitlement": "800",
            "Utilisation": "200",
            "Balance": "600"
          },
          "Optical": {
            "Entitlement": "500",
            "Utilisation": "0.00",
            "Balance": "500"
          },
          "EHS": {
            "Entitlement": "1000",
            "Utilisation": "250",
            "Balance": "750"
          }
        }
      },
      {
        "Name": "Tim",
        "Relationship": "Spouse",
        "Entitlements": {
          "GP": {
            "Entitlement": "10000",
            "Utilisation": "500",
            "Balance": "9500"
          },
          "OPS": {
            "Entitlement": "10000",
            "Utilisation": "500",
            "Balance": "9500"
          }
        }
      }
    ]
  }
]

如您所见,JSON文件在Employee和Dependents中都具有相同的嵌套,称为Entitlements,它内部有一些Maps。

Employee和Dependents的基本Model类如下:

crm_single_user_model.dart(员工模型)

class SingleUser{

  final String name, identification, company, dob;

  SingleUser({this.name, this.identification, this.company, this.dob});

  factory SingleUser.fromJson(Map<String, dynamic> ujson){

    return SingleUser(
      name: ujson["Name"].toString(),
      identification: ujson["Identification"].toString(),
      company: ujson["Company"].toString(),
      dob: ujson["DateOfBirth"].toString()
    );
  }
}

crm_dependent_list_model.dart(受抚养人的模型)

class DependantModel{

  String name;
  String relationship;

  DependantModel({this.name, this.relationship});

  factory DependantModel.fromJson(Map<String, dynamic> depjson){

    return DependantModel(
      name: depjson["Name"].toString(),
      relationship: depjson["Relationship"].toString()
    );
  }
}

我的问题是如何为权利制作模型类?

我似乎无法想出一个解决方案来创建一个包含地图内部大量地图的模型类。

在这件事上我非常感谢你的帮助。

json dart flutter
1个回答
-1
投票

试着看看那个例子

https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51

class Product {
  final int id;
  final String name;
  final List<Image> images;

  Product({this.id, this.name, this.images});
}

class Image {
  final int imageId;
  final String imageName;

  Image({this.imageId, this.imageName});
}
© www.soinside.com 2019 - 2024. All rights reserved.