2017-06-17 59 views
-3

我有一個JSON對象是這樣的: {path: "images/125/17062017/home.jpg"}。它的從REST API的到來。 我想要得到這個路徑'images/125/17062017/home.jpg'。我需要 將該路徑存儲在字符串變量中。我試過這樣。 JSON.parse(response).pathresponse.path。但是這些方法不起作用。如何將JSON值轉換爲角度4中的字符串?

fileChange(event: any) { 
    const fileList: FileList = event.target.files; 
    if(fileList.length > 0) { 
    let file: File = fileList[0]; 
    this.fileUploadService.saveOwnImageFile(file) 
     .subscribe((response: any) =>{ 
      console.log(response); // response = {path:'images/125/17062017/home.jpg'} 
      let value = JSON.parse(response); // getting error here 
      console.log(value); 
     }); 
    } 
} 
+0

response.json()? –

+0

這是爲什麼?不能只寫'let value = response.path'? –

+0

無法正常工作。我試過那種方式 –

回答

1

既然你說你想獲得價值'images/125/17062017/home.jpg',你應該使用let value = response.path;

JSON.stringify(response)將返回字符串{ "path" : "images/125/17062017/home.jpg" }

+0

我緩存我project.I洗乾淨。現在response.path正在工作,感謝robbannn –

+0

不客氣!如果你發現這個答案有用,一定要注意。 – robbannn

0

JSON.stringify() JSON.stringify() 使用JSON.stringify(..);

fileChange(event: any) { 
    const fileList: FileList = event.target.files; 
    if(fileList.length > 0) { 
    let file: File = fileList[0]; 
    this.fileUploadService.saveOwnImageFile(file) 
     .subscribe((response: any) =>{ 
      console.log(response); // response = {path:'images/125/17062017/home.jpg'} 
///////////////////////////////////////////////////////////////////////////// 
      let value = response.json().path.toString(); 
    ///// note your object is response 
///////////////////////////////////////////////////////////////////////////// 
      console.log(value); 
     }); 
    } 
}