Error data.forEach不是函数(如何提取对象的Json对象)

Error data.forEach不是函数(如何提取对象的Json对象),json,angular,Json,Angular,我正在处理一个angular项目,我必须从一个JSON对象获取数据,在正常情况下,我将创建一个类,例如'fake.ts'来获取和访问正确的值,但这里我有一个JSON,它包含4个其他对象,如: { "GetUIDriverManagementParamResult": { "AccessRightsOfCurrentAccount": [{ "Key": "3", "Value": "Gestion des comptes" }, { "Ke

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

{
  "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不是函数

那个么伙计们,例如,我如何将代码更改为从ListDriveRelation访问地址呢


请大家帮忙

据我所知,您希望得到这样的答复。因此,您可以使用
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: ""}]
 }

从API获取值时,不必提供类型。要获取嵌套对象
CurrentAccount
,可以尝试以下操作

this.\u documentmanagementservice.getDriverManager().subscribe(
(数据)=>{//{
console.log(accessRight);
});
您无法获得多个
CurrentAccount
s,因为在您提供的对象中只有一个
CurrentAccount
,其中包含的
AccessRights
是一个数组


另外,如果您没有在订阅中分配类型,请不要在
getDriverManager()中分配类型
服务中的函数。

不是一个有效的JSON结构。只是一个示例而已。我需要知道如何访问对象的对象。您是从本地文件还是从api获取JSON?并且将JSON更改为clean JSON,以便从api清楚地理解,我将显示结果图像我们在屏幕截图中看不到驱动程序在哪里您提供了。请向我们提供正确的数据,以便我们能够理解和解决我需要提取数据以避免在其上插入空对象的问题it@omarbouhajja,是的,我知道试试这个,它会起作用的。请阅读有关
对象的详细信息。条目
但它不起作用。值有{地址:“”,BirthDateLocal:null,出生地:“”,{地址:,出生日期本地:null,出生地:}作为result@SRana你能帮我吗!@omarbouhajja,你到底想要什么?根据你之前的问题,你想要访问DriverListent。