1
Below is the Array I need:
[
{
"id":1,
"nome":"LESPAUL",
"validade":1525568400000,
"dataGerada":1525914000000,
"status":true,
"itens":[
{
"cliente":{
"id":1,
"login":"gabriel",
"senha":"123",
"tipoDeLogin":1,
"telefone":"99916-2410",
"endereco":"Zetete Tororo 25",
"nome":"Gabriel Sereno",
"cpf":"12312312312"
}
}
]
}
]
Below is the Array
that is produced:
{
"id": 1,
"nome": "LESPAUL",
"validade": 1525568400000,
"dataGerada": 1525914000000,
"status": true,
"itens": [
{
"id": 1,
"login": "gabriel",
"senha": "123",
"tipoDeLogin": 1,
"telefone": "99916-2410",
"endereco": "Zetete Tororo 25",
"nome": "Gabriel Sereno",
"cpf": "12312312312"
}
]
}
You can see that I want Array
Items be an Array of Clients, only it prints the JSON without the client class, follows the code I made:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController } from 'ionic-angular';
import { ApiProvider } from '../../../providers/api/api';
import { LoaderProvider } from '../../../providers/loader';
import { ToastProvider } from '../../../providers/toast';
@IonicPage()
@Component({
selector: 'page-codigo',
templateUrl: 'codigo.html',
})
export class CodigoPage {
listClientes: any;
listCodigos: any;
codigoMarcado: any;
marca: boolean = true;
constructor(public navCtrl: NavController,
public toast: ToastProvider,
public api: ApiProvider,
public modalCtrl: ModalController,
public loader: LoaderProvider,
public navParams: NavParams) {
}
ionViewDidLoad() {
this.loader.show();
this.api.obter("clientes/codigos").then(data => {
this.loader.hide();
this.listClientes = data;
});
this.listarCodigos();
}
listarCodigos() {
this.loader.show();
this.api.obter("codigos", true).then(data => {
this.listCodigos = data;
this.loader.hide();
})
}
selecionarTodos() {
this.listClientes.forEach(data => {
data.marcado = this.marca;
})
this.marca = !this.marca;
}
adicionar() {
let modal = this.modalCtrl.create("CrudCodigoPage");
modal.present();
modal.onDidDismiss(data => {
this.listarCodigos();
})
}
enviarCodigo() {
this.loader.show();
let auxList = this.codigoMarcado.itens;
this.codigoMarcado.itens = new Array<Cliente>();
if (auxList != null) {
this.listClientes.concat(auxList);
}
this.listClientes.forEach(data => {
if (data.marcado) {
let bool = false;
for (let aux of auxList) {
if (aux.id == data.id) {
bool = true;
}
}
if (!bool) {
delete (data.marcado);
this.codigoMarcado.itens.push(new Cliente(data.id, data.login, data.senha, data.tipoDeLogin, data.telefone, data.endereco, data.nome, data.cpf));
}
}
});
// console.log(this.codigoMarcado)
this.api.put("codigos", this.codigoMarcado).then(data => {
this.toast.show("Enviado os códigos com sucesso!");
this.loader.hide();
});
}
}
export class Cliente {
id: number;
login: string;
senha: string;
tipoDeLogin: string;
telefone: string;
endereco: string;
nome: string;
cpf: string;
constructor(id: number,
login: string,
senha: string,
tipoDeLogin: string,
telefone: string,
endereco: string,
nome: string,
cpf: string) {
this.id = id;
this.login = login;
this.senha = senha;
this.tipoDeLogin = tipoDeLogin;
this.telefone = telefone;
this.endereco = endereco;
this.nome = nome;
this.cpf = cpf;
}
}
The second data structure is not an array.
– Eduardo Vargas
Do you have a model class equal to the object you want? Who returns this data?
– Guilherme Nunes
can push({client: new client(...)})
– Eduardo Vargas