Javascript 如何在angular 4中将JSON值转换为字符串?
我有一个json对象,如下所示:Javascript 如何在angular 4中将JSON值转换为字符串?,javascript,json,angular,typescript,Javascript,Json,Angular,Typescript,我有一个json对象,如下所示: {path:images/125/17062017/home.jpg}。它来自RESTAPI。 我想得到这个路径'images/125/17062017/home.jpg'。我需要 将该路径存储在字符串变量中。我试过了。 JSON.parse(response).path,response.path。但这些方法并不奏效 fileChange(event: any) { const fileList: FileList = event.target.fi
{path:images/125/17062017/home.jpg}
。它来自RESTAPI。
我想得到这个路径'images/125/17062017/home.jpg'
。我需要
将该路径存储在字符串变量中。我试过了。
JSON.parse(response).path
,response.path
。但这些方法并不奏效
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);
});
}
}
JSON.stringify()
JSON.stringify()
使用JSON.stringify(…)代码>
既然您说您希望获得值'images/125/17062017/home.jpg'
,那么您应该使用let value=response.path代码>
JSON.stringify(response)
将返回字符串{“path”:“images/125/17062017/home.jpg”}
response.JSON()?为什么?无法直接写入let value=response.path
?无法工作。我试过了有什么问题吗?你能提供一个plnker吗?@SathishKotha我的解决方案有效吗?或者需要更多帮助吗?我的项目中有缓存。我清理了它。现在response.path正在工作,谢谢Robbann不客气!如果你觉得这个答案有用,一定要投赞成票。
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);
});
}
}