错误错误:未捕获(承诺):语法错误:JSON中位置0

时间:2019-05-06 09:22:19

标签: json api ionic-framework

我正在开发Ionic应用程序。我坚持是错误的。我想获取数据JSON API地震表格 USGS,然后在Google地图上设置坐标。在控制台日志中返回json数据很好,但是当我运行我的应用ERROR Error: Uncaught (in promise): SyntaxError: Unexpected token u in JSON at position 0

时我出错

我的完整代码

export class HomePage implements OnInit {
  protected points: { lng: number, lat: number }[] = [];

  items: any
  pet: string = "Today";
  map: GoogleMap;
  mags: number;

  constructor(
    private http: HTTP) {

  }

  async ngOnInit() {

    this.getData()

  }

  async getData() {

   this.http.get(`https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson`, {}, {}).then(data => {

      this.items = JSON.parse(data.data)

      console.log(this.items)


      for (let datas of JSON.parse(data.data)['features']) {

        this.points.push({ lng: datas.geometry.coordinates[0], lat: datas.geometry.coordinates[1] });

        let dest = this.points.map((coords) => {
          return this.map.addMarkerSync({
            position: coords,
          });
        });


        this.map = GoogleMaps.create('map_canvas');
      }

    })
  }

}

JSON简短响应

{
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          35.9752,
          36.2659,
          10
        ]
      },
      "id": "us700036mi"
    }
  ]
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我再次调整了代码,即使他在Google地图上显示坐标,他也能正常工作

      async getData() {

    this.http.get(`https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&minlatitude=28.071980&minlongitude=35.683594&maxlatitude=38.203655&maxlongitude=51.108398`, {}, {}).then(data => {

      this.items = JSON.parse(data.data)
      let earth = JSON.parse(data.data)

      console.log(this.items)

    let mapOptions: GoogleMapOptions = {
      camera: {
         target: this.points,

       }
    };
    this.map = GoogleMaps.create('map_canvas',mapOptions);

      for (let datas of earth['features']) {

        this.points.push({ lng: datas.geometry.coordinates[0], lat: datas.geometry.coordinates[1] });

        let dest = this.points.map((coords) => {
          return this.map.addMarkerSync({
            position: coords,

          });
        });


      }



    })
  }