我有一些如下所示的JSON数组,我从服务中获取了这些JSON。
[{
"id": 3,
"name": "Arun"
}, {
"id": 5,
"name": "Barun"
}, {
"id": 1,
"name": "Joy"
}, {
"id": 8,
"name": "Suman"
}]
现在,我在下面做了个赞
class Person{
id:number
name:string
deserialize(input) {
this.id = input.id;
this.name = input.name
return this;
}
}
现在,我有了一个服务类,从那里可以通过Promise获取JSON(Array / dictionary),并将此Promise发送到解析函数。我的解析功能低于
private parse(response:Promise<{}>):Promise<{}>{
return new Promise((success,failure) =>{
response.then((result) => {
if (result instanceof Array || Array.isArray(result)){
let rs1 = result.map((val) => new Person().deserialize(val))
success(rs1);
}else{
success(new Person().deserialize(result))
}
},(err) => {
failure(err)
});
});
}
现在,我有两个功能
expectingObjectOfPerson(){
//get the promise from service class
let result = servicePromise//Promise from service class with JSON
//now call parse function
this.parse(result)
.then((response) => {
//I knew that response is an object of Person in run time.
let person = response as Person
//log name of this person
console.log(person.name) //no run time exception
//log name of this person
console.log(person.nam) //Error thrown by compiler
//now if I do this
console.log(response.name) //no run time exception
//if this
console.log(response.nam) //run time exception, also no error thrown from compiler
/* Main problem is on here.
* Is there any way that response could automatically inferred to Person */
}, (error) => {
Console.log(error);
})
}
expectingArrayOfPerson(){
//get the promise from service class
let result = servicePromise//Promise from service class with JSON
//now call parse function
this.parse(result)
.then((response) => {
//I knew that response is an object of Person in run time.
let persons = response as Person[]
//log name of this person
console.log(persons.map((val) => val.name)) //no run time exception
//log name of this person
console.log(persons.map((val) => val.name)) //Error thrown by compiler
//now if I do this
console.log(response.map((val) => val.name)) //no run time exception
//if this
console.log(persons.map((val) => val.name)) //run time exception , also no error thrown from compiler
/* Main problem is on here.
* Is there any way that response could automatically inferred to array of Person */
}, (error) => {
Console.log(error);
})
}
是否有任何方法可以将响应自动推断为Person对象或Person数组取决于响应? 我是Angular的新手,任何帮助将不胜感激。
我正在使用最新版本的Typescript。
答案 0 :(得分:0)
首先,让我们来看一下您的服务:您正在创建一个Promise,以处理另一个承诺。在重复自己。
此外,您应该使用Observables,它们是已升级的Promise。
最后,您每次都会创建一个新的Person对象。考虑使用静态方法反序列化。
这给您:
export function parse(response: Promise<{}>): Observable<{}> {
// Rx 6 : return fromPromise(response)
return Observable.fromPromise(response)
.map(result => {
if (result instanceof Array || Array.isArray(result)) {
return result.map((val) => Person.deserialize(val))
} else {
return Person.deserialize(result);
}
})
}
export class Person {
id: number;
name: string;
public static deserialize(input: any): Person {
const person = new Person();
person.id = input.id;
person.name = input.name;
return person;
}
}
现在您可以像这样使用两个功能:
expectingObjectOfPerson(){
this.parse(servicePromise) // Whatever it is
.subscribe(personInstance => {
console.log(personInstance);
console.log(personInstance instanceof Person);
}, error => console.log(error));
}
expectingArrayOfPerson(){
this.parse(servicePromise) // Whatever it is
.subscribe(persons => {
for (const personInstance of persons) {
console.log(personInstance);
console.log(personInstance instanceof Person);
}
}, error => console.log(error));
}