2017-06-17 8 views
-3

私はこのようなjsonオブジェクトを持っています: {path: "images/125/17062017/home.jpg"}。休憩APIから来ています。 私はこのパスを取得したい'images/125/17062017/home.jpg'。そのパスを文字列変数に格納するには が必要です。私はこのようにしてみました。 JSON.parse(response).path,response.path。しかし、これらの方法は機能しません。角度4のJSON値を文字列に変換するにはどうすればよいですか?

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

私は自分のプロジェクトにキャッシュを持っています。今、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); 
     }); 
    } 
} 
関連する問題