如何将Json响应转换为角度为5的地图?

时间:2018-03-14 15:46:25

标签: json angular5

我是新手,有点困惑, 当我调用Rest API时,获取JSON响应 但是我需要将JSON响应存储到(Map<string, string>)中, 云请帮助解决这个问题。

修改1:

在调用api/user/id时低于json响应

{
"firstname":"test",
"lastname":"testlastname",
"email":"test@test.com",
"username":"ufk",
"password":"qweasd",
"phone":"123456789"
}

2 个答案:

答案 0 :(得分:0)

<强> Service.ts:

public getJSONResponse(): Observable<Map> {
    return this.http.get<Map>('api's end point');
}

<强>型号:

export class Map{//sample Model
    firstname : string
    lastname : string
    email : string
    username : string
    password : string
    phone : string       
}

答案 1 :(得分:0)

使用下面的代码来解决,JSON响应直接发送到地图

return this.http.get('/api/user/' + ID, options)

       .map((response: Response) => {
       return response.json() as User;
      }).toPromise().then(x => {
     this.user= x;
})