How to get a string from a json?

0

I'm developing an application and need to save in a string type variable that comes in the server json. How to solve?

 export class InicioPage {
  
  users : any[];
  vaga  : any[];
  nomes : boolean = true;  
  elementType : 'url' | 'canvas' | 'img' = 'url';
  value; //= 'Código gerado';  
  constructor(public navCtrl: NavController,
              private alertCtrl: AlertController,
              public service : ServiceProviderInicio,
              //public service1 : ServiceProvider
              

            ) {
    this.value = {};
  }

  ngOnInit() {
           this.getVaga();
           this.getDados();
     }

  /*  getDados() {
     //retorno de Dados
     this.service.getData()
           .subscribe(
                 data=> this.users = data
                 ,err=> console.log(err)
           );
     }*/

     getDados() {
     //retorno de Dados
     this.service.getData()
           .subscribe(
                 data=> this.users = data
                 ,err=> console.log(err)
           );
     }
}

Excerpt performs request in database

@Injectable()
export class ServiceProviderInicio {

      api : string = 'http://localhost:80/APIEST/inicio/';


  constructor(public http: Http) {}
      getData() {
            return this.http.get(this.api + 'apiRecuperaInicio.php').map(res=>res.json())
      }
}

and query on the server

<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: text/html; charset=utf-8');
//recupera login usuario

$dns = "mysql:host=localhost;dbname=estacionamentobd";
$user = "root";
$pass = "";
//aqui

try {
	$con = new PDO($dns, $user, $pass);

	if(!$con){
		echo "Não foi possivel conectar com Banco de Dados!";
	}

	$query = $con->prepare("SELECT * FROM usuario");

		$query->execute();

		$out = "[";

		while($result = $query->fetch()){
			if ($out != "[") {
				$out .= ",";
			}
			$out .= '{"id_usuario": "'.$result["id_usuario"].'",';
			$out .= '"login": "'.$result["login"].'"}';
		}
		$out .= "]";
		echo $out;




} catch (Exception $e) {
	echo "Erro: ". $e->getMessage();
};

I need to save in the variable value as type string. Thanks

    
asked by anonymous 07.09.2017 / 00:31

1 answer

2

What you can do is getData().subscribe(res => this.value = JSON.stringify(res)) . If that's what I understood, this would be:)

    
14.09.2017 / 17:44