JSON parsing with Ionic 2 and typescript

I am trying to parse some JSON data from randomuser.me api to do this, find some tutorials on the Internet, but something seems to have changed lately in Ionic 2 because none of them work.

Here is what I have:

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Http} from '@angular/http';


@Component({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
  items : any;
  //http://api.randomuser.me/?results=10

  constructor(private navController: NavController, private http: Http) {

    this.http.get("http://api.randomuser.me/?results=10").subscribe(data => {
        console.log("Got data");
        this.items=JSON.parse(data._body).results; // this is the error
        console.log(this.items);
    });
  }

  itemClicked(event, item) {
    console.log(item.title);
    //console.log(event);
  }


}

In the terminal, I see an error: data._body - the "_body" property is private and is available only in the "Answer" class.

What can I do?

+4
source share
3 answers

data._body for data.text (),

Instead data.text(), then analyzing it, you should usedata.json()

this.items = data.json();

https://angular.io/docs/ts/latest/guide/server-communication.html#!#extract-data

+6

.

this.items= JSON.parse(data['_body']).results;

+4

Ionic 2 , , , .

,

import 'rxjs/add/operator/map';

...

        this.http.get("http://api.randomuser.me/?results=10").map(res => res.json()).subscribe(data => {
        console.log("Got data");
        console.log(this.data);
    });
}

JSON .
, .map(res = > res.json()

, Ionic 2, , API: https://www.youtube.com/watch?v=vuc4dp0qHSc&index=33&list=PLvLBrJpVwC7ocO1r-xu218C15iE9gTWBA

+1

All Articles