当我在角度2中解析我的http响应时,它不解析函数?

时间:2018-02-09 10:25:35

标签: json angular typescript jsonparser

这是我的打字稿类

import { Hours } from "./hours";


export class Day {
            public dayName: string;
            public dayId: number;
            public dayStatus: string = "Closed";
            public checked: boolean = false;
            public hours: Hours[] = new Array();


consturctor() {

}


public equals( dayid: number ): boolean {

    if ( this.dayId == dayid ) {
        return true;
    }

    return false;
}

public isChecked() {
    let result: boolean;
    if ( this.dayStatus == "Open" ) {
        result = true;
    }
    else {
        result = false;
    }
    return result;
}
}

当我将此对象发送到我的休息服务时,它在java spring中将对象保存到数据库中但是当我从数据库中获取此对象时,我得到所有字段但是当我尝试访问equals方法时,我得到等于不是方法的错误。响应的 proto 低于

      __proto__
     :
      constructor
      :
    ƒ Object()
    hasOwnProperty
    :
    ƒ hasOwnProperty()
    isPrototypeOf
    :
    ƒ isPrototypeOf()
    propertyIsEnumerable
    :
    ƒ propertyIsEnumerable()
    toLocaleString
    :
    ƒ toLocaleString()
    toString
    :
   ƒ ()
     valueOf
   :
   ƒ valueOf()
  __defineGetter__
   :
   ƒ __defineGetter__()
  __defineSetter__
   :
   ƒ __defineSetter__()
   __lookupGetter__
     :
    ƒ __lookupGetter__()
    __lookupSetter__
     :
    ƒ __lookupSetter__()
    get __proto__
    :
     ƒ __proto__()
    set __proto__
     :
      ƒ __proto__()

它没有相同的方法。如果我做一个新的一天();我得到以下相同的方法是我得到请求的代码

       this.http.get(url,options).map((res)=>res.json()).
       subscribe((data)=>{ obj=data});

**编辑**

这是我发送给服务器的json

    Day
    checked: true
    dayId:1
    dayName:"Monday"
    dayStatus:"Open" 
    hours:[Hours]
    __proto__:
     consturctor:ƒ ()
     equals:ƒ (dayid)
     isChecked:ƒ ()
     constructor:ƒ Day() 
    __proto__:Object

它在请求正文中变为

          {
  "dayStatus": "Open",
  "checked": true,
  "hours": [
    {
      "startAt": "00:00",
      "closeAt": "23:59",
      "maxPatientNo": "20"
    }
  ],
  "dayId": 4,
  "dayName": "Thursday"
}

我得到了所有的领域。

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的,通常是你

  • 与客户端的数据进行交互
  • 将数据发送到您的服务器(用于验证/持久性等)

稍后在服务器中查询此数据时,您会收到数据。行为不是通过网络传输的,如果需要,你可以从该数据构建一个新的类实例。

例如,使用以下实现:

export class Day {
    public dayName: string;
    public dayId: number;
    public dayStatus: string;
    public checked: boolean;
    public hours: Hours[];


    // the default constructor
    constructor(name: string, id: number, status?: string, checked?: boolean, hours?: Hours[]) {
        this.dayName = name;
        this.dayId = id;
        this.dayStatus = status || 'closed';
        this.checked = checked || false;
        this.hours = hours || [];
    }

    // static method that calls the constructor
    // this is just like a constructor overload in other languages
    public static fromObject(object: any) {
        const {dayName, dayId, dayStatus, checked, hours} = object;
        return new this(dayName, dayId, dayStatus, checked, hours);
    }


    public equals( dayid: number ): boolean {

        if ( this.dayId == dayid ) {
            return true;
        }

        return false;
    }

    public isChecked() {
        let result: boolean;
        if ( this.dayStatus == "Open" ) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }

}

您可以像这样使用它:

this.http.get(url, options)
    .map(res => res.json())
    .map(parsed => Day.fromObject(parsed))
    .subscribe(day => this.receivedDay = day);