Ali Ghassan
Ali Ghassan

Reputation: 1180

ionic 4 native http get empty array

l am working on ionic 4 project. l am try to get data json array from url using native http for ionic . When l try to get data array from url json l got empty page ! .

data json l have is too long , l put here short json to show scheme

{
  "result": {
    "airprotname": {
      "code": "BGW"
    },
    "response": {
      "airport": {
        "plugin: {
          "schedule": {
            "arrival": {
              "data": [
                {
                  "flight": {
                    "identification": {
                      "id": iaw445,
                      "row": 4861006050
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

my code

  export class HomePage {

      public items : any[] ; 
      constructor(private http: HTTP) {



        this.http.get('/airport.json?code=BGW', {}, {})
        .then(res=> {

          this.items = JSON.parse(res.data.result.response.airport.plugin.schedule.arrival.data)
        })
        .catch(error => {



        });

      }



    }

html

<ion-content>

   {{status}}
   <ion-list>
    <ion-item *ngFor="let item of items">
      {{item.flight.identification.id}}
      <div class="item-note" slot="end">
      </div>
    </ion-item>
  </ion-list>
</ion-content>

doc for native http

Upvotes: 1

Views: 651

Answers (1)

AVJT82
AVJT82

Reputation: 73377

parse the intial response you get, before trying to access your nested data, so do...

this.http.get('/airport.json?code=BGW', {}, {})
  .then(res=> {
     const parsed = JSON.parse(res.data);
     this.items = parsed.result.response.airport.plugin.schedule.arrival.data;
  })

Upvotes: 1

Related Questions