获取 json 文件的内容无效
Get contents of json file not working
我想使用 angular 2 http.get
从 json 文件中获取 json 对象。我最终从文件中得到的是:
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
使其 return 成为 json 对象而不是这个:t_isScalar: falseoperator: tsource: t__proto__: Object
。请注意,当我更改文件名时,它 return 是一样的。我本以为会出现信息错误(我确实进行了错误处理,但代码永远不会出错)。
您需要 subscribe
才能观察到:
@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 的回答之外,您还可以使用 | async
管道为您进行订阅。
<div>{{getSchmea('fileName') | async}}</div>
我想使用 angular 2 http.get
从 json 文件中获取 json 对象。我最终从文件中得到的是:
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
使其 return 成为 json 对象而不是这个:t_isScalar: falseoperator: tsource: t__proto__: Object
。请注意,当我更改文件名时,它 return 是一样的。我本以为会出现信息错误(我确实进行了错误处理,但代码永远不会出错)。
您需要 subscribe
才能观察到:
@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 的回答之外,您还可以使用 | async
管道为您进行订阅。
<div>{{getSchmea('fileName') | async}}</div>