最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert JSON value to string in angular 4? - Stack Overflow

programmeradmin0浏览0评论

I have a json object like this: {path: "images/125/17062017/home.jpg"}.It's ing from rest api. I want to get this path 'images/125/17062017/home.jpg'. And I need to store that path in string variable. I tried like this. JSON.parse(response).path, response.path. But these ways are not working.

 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);
      });
     }
}

I have a json object like this: {path: "images/125/17062017/home.jpg"}.It's ing from rest api. I want to get this path 'images/125/17062017/home.jpg'. And I need to store that path in string variable. I tried like this. JSON.parse(response).path, response.path. But these ways are not working.

 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);
      });
     }
}
Share Improve this question edited Jun 17, 2017 at 18:16 Sathish Kotha asked Jun 17, 2017 at 17:40 Sathish KothaSathish Kotha 1,1113 gold badges17 silver badges31 bronze badges 5
  • response.json()? – Edmundo Santos Commented Jun 17, 2017 at 17:42
  • why's that ? couldnt just write let value = response.path ? – SeleM Commented Jun 17, 2017 at 17:49
  • not working. i tried that ways – Sathish Kotha Commented Jun 17, 2017 at 17:51
  • what is the problem ? could you provide a plnker please ? – SeleM Commented Jun 17, 2017 at 17:52
  • @SathishKotha did my solution worked or need more help – Aravind Commented Jun 17, 2017 at 18:13
Add a ment  | 

2 Answers 2

Reset to default 3

Since you say you would like to get the value 'images/125/17062017/home.jpg', you should use let value = response.path;.

JSON.stringify(response) would return the string { "path" : "images/125/17062017/home.jpg" }.

JSON.stringify() JSON.stringify() Use 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);
      });
     }
}
发布评论

评论列表(0)

  1. 暂无评论