首页 > 解决方案 > 错误 data.forEach 不是函数(如何提取对象的 Json 对象)

问题描述

我正在做一个角度项目,我必须从 JSON 对象中获取数据,在正常情况下,我将创建一个类,例如“fake.ts”来获取和访问正确的值,但这里我有一个包含 4 个的 JSON其他对象,例如:

{
  "GetUIDriverManagementParamResult": {
    "AccessRightsOfCurrentAccount": [{
      "Key": "3",
      "Value": "Gestion des comptes"
    }, {
      "Key": "4",
      "Value": "Geofencing"
    }, {
      "Key": "6",
      "Value": "Paramétrage des alertes"
    }, {
      "Key": "9",
      "Value": "Gestion des emplacements"
    }, {
      "Key": "11",
      "Value": "Génération des rapports et consultation d'historique"
    }, {
      "Key": "12",
      "Value": "Suivi général"
    }, {
      "Key": "14",
      "Value": "Gestion des chauffeurs"
    }, {
      "Key": "15",
      "Value": "Gestion des véhicules"
    }, {
      "Key": "17",
      "Value": "Gestion des départements"
    }, {
      "Key": "18",
      "Value": "Contrôle des unités"
    }, {
      "Key": "19",
      "Value": "Suivi des alertes"
    }, {
      "Key": "22",
      "Value": "Envoi des alertes par Mail/SMS"
    }, {
      "Key": "23",
      "Value": "Gestion de maintenance"
    }, {
      "Key": "25",
      "Value": "Gestion des documents"
    }, {
      "Key": "26",
      "Value": "Gestion des bons et des cartes de carburant"
    }, {
      "Key": "27",
      "Value": "Gestion des sinistres"
    }, {
      "Key": "28",
      "Value": "Gestion des ordres de mission"
    }, {
      "Key": "29",
      "Value": "Gestion d’emprunt et de location"
    }, {
      "Key": "61",
      "Value": "Gestion des fournisseurs"
    }, {
      "Key": "62",
      "Value": "Gestion des bénéficiaires"
    }, {
      "Key": "63",
      "Value": "Gestion des articles  et des stocks"
    }, {
      "Key": "64",
      "Value": "Gestion des bons/factures et payements"
    }, {
      "Key": "65",
      "Value": "Dépenses générales"
    }, {
      "Key": "66",
      "Value": "Gestion des pneus"
    }],
    "ConnectionInfo": null,
    "CurrentAccount": {
      "AccessRights": [{
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 3
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 4
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": false,
        "Page": 5
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 6
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 9
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 11
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 12
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 14
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 15
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 17
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 18
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 19
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 22
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 23
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 25
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 26
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 27
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 28
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 29
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": false,
        "Page": 56
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": false,
        "Page": 57
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": false,
        "Page": 58
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": false,
        "Page": 59
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": false,
        "Page": 60
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 61
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 62
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 63
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 64
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 65
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 66
      }, {
        "__type": "Account_Page:#Objects",
        "AccessRight": true,
        "Page": 68
      }],
      "ApplicationType": 16,
      "Customer": {
        "AccountsCount": -1,
        "Adress": null,
        "AuthorizedSMSCount": 0,
        "AuthorizedSMSCountString": "",
        "AutoLockCtmByResInDay": 0,
        "AutoLockCtmByResInDayStatusEnumValue": -1,
        "AutoLockCtmByTnvInDay": 0,
        "AutoLockCtmByTnvInDayStatusEnumValue": -1,
        "AutoShowMsg4PaymentInvoiceByResInDay": 0,
        "AutoShowMsg4PaymentInvoiceByTnvInDay": 0,
        "AutoShowMsg4PaymentInvoiceStatusEnumByResValue": -1,
        "AutoShowMsg4PaymentInvoiceStatusEnumByTnvValue": -1,
        "CanAddCars": false,
        "CarCount": 150,
        "ConditionalSpeedForcedArmKmh": 0,
        "Config": 6144,
        "CustomerLockedStateEnum": 0,
        "CustomerStatus": 1,
        "CustomerStatusEnum": 1,
        "CustomerType": 0,
        "DownloadPassword": null,
        "Email1": null,
        "Email2": null,
        "Email3": null,
        "ExpirationDateForResellerGMT": "/Date(-2206314000000+0100)/",
        "ExpirationDateForTnvGMT": "/Date(-2206314000000+0100)/",
        "ExpirationDateStatusEnumValueForReseller": 0,
        "ExpirationDateStatusEnumValueForTunav": 0,
        "GMT": 0,
        "Id": 350,
        "IsAuthorizedSMSEnable": false,
        "IsAuthorizedToUseGooglePlanMaps": true,
        "IsAuthorizedToUseGoogleSatelliteMaps": true,
        "IsAuthorizedToUseHereMaps": true,
        "IsAuthorizedToUseOSMMaps": true,
        "IsAuthorizedToUseTunavMaps": true,
        "IsEnableLoadDelegation": false,
        "IsGeocodingFromGoogleSvcAndPlace": true,
        "IsGeocodingFromOSMSvcAndPlace": false,
        "IsGeocodingFromSTDPlaceAndPlace": false,
        "IsGeocodingFromTunavSvcAndPlace": false,
        "IsLocked": false,
        "IsReseller": false,
        "IsTunavAdmin": false,
        "LastAutoMsg4PaymentInvoiceShowedByResState": false,
        "LastAutoMsg4PaymentInvoiceShowedByTnvState": false,
        "LastConnectionDateGMT": "/Date(1585578902032)/",
        "LastConnectionInfo": "Lg:adminetap; Pw:QGRtaW4qRXRhcDA=; Ip:; Cn:; App:Web; AppResName:Tnv",
        "LastConnectionStatusEnumValue": 0,
        "LastLockedCtmByResState": false,
        "LastLockedCtmByTnvState": false,
        "LastLockedManuallyCtmByResState": false,
        "LastLockedManuallyCtmByTnvState": false,
        "MailForAllCar": false,
        "MarketingFormulaContractValue": 0,
        "MarketingFormulaContractValueString": "Abonnement",
        "MessageToShow": null,
        "Name": "SNBG",
        "NbrAuthorizedSMSBySingleNumber": 0,
        "NbrEq": 13,
        "NbrEqConnectedFromXTime": 0,
        "NbrEqDisconnectedMoreThanXTime": 0,
        "Reseller": 351,
        "SendMailToCtmWhenAutoMsg4PaymentInvoiceShowed": true,
        "SendMailToResWhenAutoCtmLocked": true,
        "SendMailToResWhenAutoMsg4PaymentInvoiceShowed": true,
        "SendMailToTnvWhenAutoCtmLockedByRes": true,
        "SendMailToTnvWhenAutoCtmLockedByTnv": true,
        "SendMailToTnvWhenAutoCtmLockedIgnoredByTnv": true,
        "SendMailToTnvWhenAutoCtmUnlockedByRes": true,
        "SendMailToTnvWhenAutoCtmUnlockedByTnv": true,
        "SendMailToTnvWhenAutoMsg4PaymentInvoiceShowed": true,
        "SendSMSToCtmWhenAutoMsg4PaymentInvoiceShowed": false,
        "SendSMSToResWhenAutoCtmLocked": false,
        "SendSMSToResWhenAutoMsg4PaymentInvoiceShowed": false,
        "SendSMSToTnvWhenAutoCtmLockedByRes": true,
        "SendSMSToTnvWhenAutoCtmLockedByTnv": true,
        "SendSMSToTnvWhenAutoCtmLockedIgnoredByTnv": true,
        "SendSMSToTnvWhenAutoCtmUnlockedByRes": true,
        "SendSMSToTnvWhenAutoCtmUnlockedByTnv": true,
        "SendSMSToTnvWhenAutoMsg4PaymentInvoiceShowed": true,
        "SiteName": "ETAP",
        "SmsForAllCar": false,
        "SubcriptionWebAccountStatusEnumForReseller": 2,
        "SubcriptionWebAccountStatusEnumForTunav": 2,
        "SubscriptionDate": "/Date(-2206314000000+0100)/",
        "Tel1": null,
        "Tel2": null,
        "Tel3": null,
        "TimeOutToSendCommandMn": 0,
        "URL": null,
        "UseCustomGrpSMSModem": false,
        "Website": null,
        "m_Config": 6144
      },
      "Departments": [],
      "EnablePlaceClusterer": false,
      "EnableTrakingObjClusterer": false,
      "ExternalId": 0,
      "ExternalId2": 0,
      "Id": 5,
      "IsReseller": false,
      "IsTunavAdmin": false,
      "LoadAllPlacesInMap": false,
      "Login": "adminetap",
      "MapZoomToFocusObjLevel": 14,
      "MinPeriodToDetectStpInS": 0,
      "OptionsConfig": 128,
      "Password": "4FAB83B5B6481B5447B0B5D86313E3DFD1E640AD",
      "ReselerApp": 0,
      "ShowNotAuthrorisedVehiculeInBrowser": true,
      "ShowedAlarmInRTCfg": 0,
      "TrackPageConfigStr": "FH??G??????D?????????????????????????????????E??????????????@{B]B??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????",
      "ViewAllTrackingObjects": true,
      "m_Role": "Unknown",
      "m_TrackPageConfigStr": "FH??G??????D?????????????????????????????????E??????????????@{B]B??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????"
    },
    "ListDepartment": [{
      "Customer": 350,
      "ExternalId": 0,
      "ExternalId2": 0,
      "FullName": null,
      "Id": 1,
      "ListIdTrackingObj": [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 16, 17, 1017],
      "Name": "Dep1",
      "ParentDep": 1024,
      "Type": 0
    }, {
      "Customer": 350,
      "ExternalId": 0,
      "ExternalId2": 0,
      "FullName": null,
      "Id": 1054,
      "ListIdTrackingObj": [6],
      "Name": "Dep3",
      "ParentDep": 1053,
      "Type": 0
    }, {
      "Customer": 350,
      "ExternalId": 0,
      "ExternalId2": 0,
      "FullName": null,
      "Id": 1055,
      "ListIdTrackingObj": [15],
      "Name": "Dep2",
      "ParentDep": 1026,
      "Type": 0
    }, {
      "Customer": 350,
      "ExternalId": 0,
      "ExternalId2": 0,
      "FullName": null,
      "Id": 1070,
      "ListIdTrackingObj": null,
      "Name": "depMinistere",
      "ParentDep": 1030,
      "Type": 0
    }, {
      "Customer": 350,
      "ExternalId": 0,
      "ExternalId2": 0,
      "FullName": null,
      "Id": 1071,
      "ListIdTrackingObj": null,
      "Name": "قسم",
      "ParentDep": 1024,
      "Type": 0
    }],
    "ListDriverElement": [{
      "Address": "",
      "BirthDateLocal": null,
      "BirthPlace": "",
      "CIN": "",
      "CardCouponReminderJSON": null,
      "CardCouponReminderJSONToDo": null,
      "CurrentTransportationId": 1017,
      "CurrentTransportationName": "2936 TU 164 Abdelhafidh Bahri",
      "DepartmentFullString": null,
      "DepartmentId": 1,
      "DepartmentString": "Dep1",
      "DriverCode": null,
      "DriverCodeInt": -1,
      "DriverLicenseExpirationDateLocal": null,
      "DriverLicenseNumber": "",
      "DriverLicenseType": "",
      "DriverStatus": "Occupé",
      "Email": "",
      "ExistCardCouponReminderForThisDriver": false,
      "FirstName": "Ch1",
      "FullName": "Ch1 Ali",
      "IdCardCouponLogJSON": null,
      "IsFree": false,
      "LastName": "Ali",
      "Tel": "",
      "UrlScannedCINBack": null,
      "UrlScannedCINFront": null,
      "UrlScannedDriverLicenseBack": null,
      "UrlScannedDriverLicenseFront": null,
      "key": 1
    }, {
      "Address": "tunis",
      "BirthDateLocal": "/Date(1399672800000+0100)/",
      "BirthPlace": " ",
      "CIN": " ",
      "CardCouponReminderJSON": null,
      "CardCouponReminderJSONToDo": null,
      "CurrentTransportationId": null,
      "CurrentTransportationName": "--",
      "DepartmentFullString": null,
      "DepartmentId": 1054,
      "DepartmentString": "Dep3",
      "DriverCode": "",
      "DriverCodeInt": -1,
      "DriverLicenseExpirationDateLocal": null,
      "DriverLicenseNumber": " ",
      "DriverLicenseType": " ",
      "DriverStatus": "Libre",
      "Email": "test@mail.com",
      "ExistCardCouponReminderForThisDriver": false,
      "FirstName": "chauf",
      "FullName": "chauf chauf2",
      "IdCardCouponLogJSON": null,
      "IsFree": true,
      "LastName": "chauf2",
      "Tel": "222222",
      "UrlScannedCINBack": "",
      "UrlScannedCINFront": "",
      "UrlScannedDriverLicenseBack": "",
      "UrlScannedDriverLicenseFront": "",
      "key": 3
    }],
    "Theme": "arctic"
  }
}

好的,类似的东西我试图制作一个像这样的类:

export interface Drivermanager{
AccessRightsOfCurrentAccount: any;
ConnectionInfo: any[];
CurrentAccount: any[];
ListDepartment: any[];
ListDriverElement: any[];
}

并通过以下方式获取数据:

  this._documentmanagmentService.getDriverMAnnager().subscribe((data: Drivermanager[])=>{
    console.log(data);
    data.forEach(element => {
      console.log(element.CurrentAccount); 
   });
});

但我得到了:

错误类型错误:data.forEach 不是函数

那么伙计们,例如,我如何更改我的代码以从 ListDriverElement 访问地址。

api 日志的结果

PLZ大伙帮忙!

标签: jsonangular

解决方案


据我了解,您期​​待这样的回应。因此,您可以在此基础上使用Object.entries和使用foreach。这将为您提供所需的结果。

  this._documentmanagmentService.getDriverMAnnager().subscribe((data)=>{
    Object.entries(data.ListDriverElement).forEach(([key,value]) => {
     console.log(value);
   });
});

假设你有这样的数据。

    let data = {
      AccessRightsOfCurrentAccount: null,
      ConnectionInfo: [],
      CurrentAccount: [],
     ListDepartment: [],
     ListDriverElement: [{Address: "", BirthDateLocal: null, BirthPlace: ""}, 
    {Address: "", BirthDateLocal: null, BirthPlace: ""}]
 }

推荐阅读