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
2 Answers
Reset to default 3Since 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);
});
}
}