-1

I'm working on an angular project and I have to get data from a JSON object, in normal cases I will create a class for example 'fake.ts' to fetch and access to right values but here I have a JSON that contains 4 other objects like:

{
  "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"
  }
}

ok, Something Like that I have tried to make a class like :

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

and fetch data with :

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

but I got:

ERROR TypeError: data.forEach is not a function

then Guys how can I change my code to access Address from ListDriverElement for example.

the result of api log

PLz guys help!

ruth
  • 29,535
  • 4
  • 30
  • 57
omar bouhajja
  • 105
  • 1
  • 1
  • 11

2 Answers2

0

As i understood, you are expecting a response like this. So you can use Object.entries and use foreach on top of that. This will give you the desired result.

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

Assumed you have a data like this.

    let data = {
      AccessRightsOfCurrentAccount: null,
      ConnectionInfo: [],
      CurrentAccount: [],
     ListDepartment: [],
     ListDriverElement: [{Address: "", BirthDateLocal: null, BirthPlace: ""}, 
    {Address: "", BirthDateLocal: null, BirthPlace: ""}]
 }
uiTeam324
  • 1,215
  • 15
  • 34
0

You don't necessarily have to provide a type when obtaining values from APIs. To obtain the nested object CurrentAccount, you could try the following

this._documentmanagmentService.getDriverMAnnager().subscribe(
  (data) => { // <-- type information not provided here
  data.GetUIDriverManagementParamResult.CurrentAccount.AccessRights.forEach(accessRight => {
    console.log(accessRight); 
  });

You cannot obtain multiple CurrentAccounts because in the object you provided there is only one CurrentAccount and the AccessRights contained within it is an array.

Also if you aren't assigning types in the subscription, don't assign types in the getDriverMAnnager() function in the service.

ruth
  • 29,535
  • 4
  • 30
  • 57