JSON到Angular中的Class对象 [英] JSON to Class Object in Angular

查看:287
本文介绍了JSON到Angular中的Class对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有默认值的模型类

I have a model class with default values

export class Person {
    _index : string ="hello";
    _type : string;
    _id : string;
    _score : number;
    _source : Source = new Source();
}
export class Source {
    name : string;
    age : number = 0;
    salary : number;
    details : Details = new Details();
}

export class Details{
    year : number = 1997;
    education : Education = new Education;
}


export class Education{
    score:number = 98;
}

当我创建实例per : Person = new Person ();时,这将建立一个对象.

This builds up an object when I create an instance per : Person = new Person ();.

{
"_index":"hello",
"_source":{
"age":0,
"details":{
"year":1997,
"education":{
"score":98
}
}
}

现在我已从模型中的服务器获取JSON模型

Now I have got JSON Model from server in the model

}
"_index":"person",
"_type":"single",
"_id":"AWCoCbZwiyu3OzMFZ_P9",
"_version":2,
"found":true,
"_source":{
"name":"sauravss",
"age":"21",
"salary":"50000"
}
}

我想将值填充到我的类对象中,但是当我用结果订阅我的类对象时,它会将我的类对象更改为JSON对象的形式,并消除了默认值,从而为我提供了从服务器接收到的Model上面的JSON.订阅后,我会在per中获得此表格.

I want to fill the values to my class object but when I subscribe my class object with the result, it changes my class object to the form of JSON object and eliminating the default values giving me the Model that I received from server as the just above JSON. I get this form in per after I subscribe.

我想要的是JSON对象,必须用与其匹配的字段填充类Object,而不匹配的字段必须包含默认值.

What I want is the JSON object must fill the class Object with the fields it matches and the unmatched fields must contain the default values.

editPerson():void {
    const id : string = this.route.snapshot.paramMap.get('id');
    console.log(id);
    this.personService.getPerson(id).subscribe(person => {
      this.per = person;
    });
  }

  getPerson (id : string): Observable<Person> {
    const url = `${this.url}/${id}`;
    console.log(id);
    return this.http.get<Person>(url);
  }

推荐答案

现在可以解决此问题.如果有更好的解决方案,请[评论].

This is the work around for now.This would help who are new to angular. If there is a better solution Please [comment].

export class Person {
    _index : string;
    _type : string;
    _id : string;
    _source : Source = new Source();

    constructor (res : Person){
        this._id = res._id;
        this._index = res._index;
        this._type = res._type;
        if(res._source){
            this._source.name = res._source.name;
            this._source.age = res._source.age;
            this._source.salary = res._source.salary;
            if(res._source.details){
                this._source.details.year = res._source.details.year;
                if(res._source.details.education){
                    this._source.details.education = res._source.details.education;
                }
            }
        }
    }
}
export class Source {
    name : string = '';
    age : number = 0;
    salary : number = 0;
    details : Details = new Details();
}

export class Details{
    year : number = 1997;
    education : Education = new Education;
}


export class Education{
    score:number = 98;
}

这篇关于JSON到Angular中的Class对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆