我有一个java web服务表示出来提出的Json的列表,这个对象的属性>Convertin res.json()的阵列<Object>
public class Oferta {
private int id;
private String categoria;
private String descricao_oferta;
private String anunciante;
private double valor;
private boolean destaque;
private List<String> imagens;
}
而且我有我想取出这个JSON一个Angular4项目并存储在一个数组中。我应该怎么做?
Angular4 oferta型号:
export class Oferta {
public id: number;
public categoria: string;
public titulo: string;
public descricao_oferta: string;
public anunciante: string;
public valor: number;
public destaque: boolean;
public imagens: Array<Object>;
}
的方法,我可以检索JSON对象的列表(工作正常,当我CONSOLE.LOG(的getData)我收到JSON对象列表:
public getData(): Promise<Oferta[]>{
this.ofertas = this.http.get(this.apiURL)
.map((res: Response) => <Oferta[]>res.json()) << ERROR WHEN CASTING
return new Promise((resolve,reject)=>{
let deu_certo = true
if(deu_certo){
setTimeout(() => resolve(this.ofertas),3000);
}
else{
reject({codigo_erro: 404,mensagem_erro: 'Servidor nao encontrado'})
}
//console.log('passou aqui')
})
.then((ofertas: Oferta[]) => {
console.log('segundo then')
return new Promise((resolve2,reject2) => {
setTimeout(() => {resolve2(ofertas)},3000)
})
})
.then((ofertas: Oferta[]) => {
console.log('terceiro then executado apos 3 sec , aguardando outra promisse ser resolvida')
return ofertas;
})
}
现在,我怎么将它转换为一个数组已经尝试过this.oferta [] = res.json();?不会让我
/////////////这是。我如何在home.com打电话
ngOnInit(){ this.ofertas = this.ofertasService.getData()
this.ofertasService.getOfertas2()
.then(
(ofertas: Oferta[]) => { this.ofertas = ofertas
console.log('a funçao resolve() foi resolvida depois de 3 segundos')
})
.catch((param: any) => {
console.log(param)
})
}
这是一个javascript/typescript问题。与Java无关 –