admin管理员组文章数量:1356225
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);
});
}
}
本文标签: javascriptHow to convert JSON value to string in angular 4Stack Overflow
版权声明:本文标题:javascript - How to convert JSON value to string in angular 4? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744038759a2580269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论