Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取json文件的内容无效_Json_Angular_Angular2 Http - Fatal编程技术网

获取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 {

我想使用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以使其返回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>