获取json文件的内容无效
我想使用angular 2 http.get从json文件中获取json对象。我最终从文件中得到的是:获取json文件的内容无效,json,angular,angular2-http,Json,Angular,Angular2 Http,我想使用angular 2 http.get从json文件中获取json对象。我最终从文件中得到的是: t_isScalar: falseoperator: tsource: t__proto__: Object 这是我的密码 @Injectable() export class ValidateJSONSchemaService { constructor(private http: Http) { } getSchema(fileName): any {
t_isScalar: falseoperator: tsource: t__proto__: Object
这是我的密码
@Injectable()
export class ValidateJSONSchemaService {
constructor(private http: Http) { }
getSchema(fileName): any {
return(this.http.get(fileName)
.map(this.extractData)
);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
}
如何修复getSchema以使其返回json对象,而不是:t_isScalar:falseoperator:tsource:t_uuProto_uu:object。请注意,当我更改文件名时,它返回相同的内容。我本以为会出现信息性错误,但我确实进行了错误处理,但代码从未出错。您需要订阅observable:
@Injectable()
export class ValidateJSONSchemaService {
constructor(private http: Http) { }
getSchema(fileName): any {
return(this.http.get(fileName)
.map(this.extractData).subscribe(data => console.log(data));
);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
}
您需要订阅observable:
@Injectable()
export class ValidateJSONSchemaService {
constructor(private http: Http) { }
getSchema(fileName): any {
return(this.http.get(fileName)
.map(this.extractData).subscribe(data => console.log(data));
);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
}
除了Maciej的答案之外,您还可以使用异步管道为您进行订阅
<div>{{getSchmea('fileName') | async}}</div>
除了Maciej的答案之外,您还可以使用异步管道为您进行订阅
<div>{{getSchmea('fileName') | async}}</div>