如何调用在Angular 2中返回单个JSON对象的web服务?

如何调用在Angular 2中返回单个JSON对象的web服务?,json,web-services,typescript,angular,Json,Web Services,Typescript,Angular,尝试从Angular 2调用返回单个Json对象的Web服务: { "fullDisplayName": "test", "result": "SUCCESS" } 以下是我的路线的代码片段: 服务 getJsonObject () { return this.http.get(this.url) .map(res => <jsonObject> res.json()) .do

尝试从Angular 2调用返回单个Json对象的Web服务:

{
   "fullDisplayName": "test",
   "result": "SUCCESS"
}
以下是我的路线的代码片段:

服务

getJsonObject () {
    return this.http.get(this.url)
                    .map(res => <jsonObject> res.json())
                    .do(jsonObject=> console.log(jsonObject))
                    .catch(this.handleError);
}
非常感谢您的帮助

     console.log(this.jsonObject)
     console.log("AFTER")
在向
this.url
发送请求之前执行
Observable
是异步的,执行被排入事件队列,当响应到达时,将执行传递给
subscribe(…)
的回调

这是您实际需要做的:

getJsonObject() {
   console.log('BEFORE')
   this.service.getJsonObject()
      .subscribe(
          jsonObject=> {
              this.jsonObject = jsonObject,
              console.log(this.jsonObject)
              console.log("AFTER")
          },
          error =>  this.errorMessage = <any>error);
 }
getJsonObject(){
console.log('BEFORE')
this.service.getJsonObject()
.订阅(
jsonObject=>{
this.jsonObject=jsonObject,
log(this.jsonObject)
console.log(“之后”)
},
error=>this.errorMessage=error);
}

什么是
jsonObject
或者它看起来怎么样?看起来像是@GünterZöchbauer的复制品,jsonObject看起来像这样<代码>导出类JsonObject{constructor(public fullDisplayName:string,public result:string){}啊,我明白了,这很有意义。但是它仍然没有显示在我的HTML中。{{jsonObject.result}}即使日志显示对象正在填充,也不会输出任何数据。是否打印到控制台?控制台中是否有错误?我想您需要
{{jsonObject?.result}}
请参见我在您问题下方的评论中的链接。就是这样。它只需要一个问号!我以前创建过使用过的web服务,但它们总是返回不需要的数组?待参考。谢谢你的帮助。
     console.log(this.jsonObject)
     console.log("AFTER")
getJsonObject() {
   console.log('BEFORE')
   this.service.getJsonObject()
      .subscribe(
          jsonObject=> {
              this.jsonObject = jsonObject,
              console.log(this.jsonObject)
              console.log("AFTER")
          },
          error =>  this.errorMessage = <any>error);
 }