从文件解析Json

时间:2017-01-20 11:36:42

标签: json angular

我有解析json的服务

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class TestService {

  private tsUrl = './../Test/test.json';
  data: Object;

  constructor(private http: Http) {}

  getTest(): Observable<any>  {
    return this.http.get(this.tsUrl)
      .map(res => res.json());
  }
}

但是当我在我的组件中调用它时

  private p: any;

  constructor(private testService : TestService ) {
    this.p =this.testService .getTest().subscribe(
      data => {
        console.log(data);
      };

    console.log(this.p);
 }

它记录: enter image description here

我该怎么办?

我正在从json文件解析吗?文件路径正确

@echonax解决了我的问题,但现在我看到了这条错误消息

enter image description here

0 个答案:

没有答案