【问题标题】:JSON to Class Object in AngularJSON到Angular中的类对象
【发布时间】:2018-02-01 17:54:51
【问题描述】:

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

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 (); 时,这会构建一个对象。

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

现在我已经从模型中的服务器获得了 JSON 模型

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

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

我想要的是 JSON 对象必须用它匹配的字段填充类 Object 并且不匹配的字段必须包含默认值。

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);
  }

【问题讨论】:

标签: json angular typescript


【解决方案1】:

您需要明确地将您的 Json 对象转换为您需要的类。

你可以在构造函数中这样做:

export class Person() {
    // ...
    constructor(jsonString: string) {
        const jsonData = JSON.parse(jsonString);
        // do your assignment from jsonData
    }
}

【讨论】:

  • 是的,这是一种解决方案,但有没有更简单的直接类型转换方法?
  • 没有。 JS 怎么会知道将哪个 Json 值分配给您的哪个字段?
  • 好的,谢谢 ritaj。我刚接触 JS 的基础知识,因为我是 JS 新手!!
  • 没问题。您应该接受解决您问题的答案。
  • “JS 怎么会知道将哪个 Json 值分配给你的哪个字段?” - 它们具有相同的名称?
【解决方案2】:

这是目前的解决方法。这将有助于刚接触 Angular 的人。如果有更好的解决方案请[评论]。

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;
}

【讨论】:

  • 在构造函数中执行它是一个可怕的想法 - 向构造函数传递一个它自己的类的实例不是很奇怪吗?创建一个静态方法或独立函数,并将每个值作为单独的参数传递给构造函数,或者只使用 Object.assign 将 JSON 中的字段添加到类的新实例中。
猜你喜欢
  • 2017-09-25
  • 2021-11-04
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多