json - Angular 5,rxjs 映射到 json,类型“对象”上不存在 'json'

标签 json angular ionic-framework rxjs rxjs5

尝试关注在线视频,然后出现这个,我是 Angular 新手,其他解决方案对我没有帮助。

enter image description here

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';

/*
  Generated class for the WeatherProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class WeatherProvider {
  apikey='7d2dc7a226a78c14';
  url;

  constructor(public http: HttpClient) {
    console.log('Hello WeatherProvider Provider');
    this.url='http://api.wunderground.com/api/'+this.apikey+'/conditions/q'
  }

    getWeather(city,state){
      return this.http.get(this.url+'/'+state+'/'+city+'.json')
        .map(res => res.json() );      
    }
}

最佳答案

如果您使用新的 HttpClient,则不需要解析 JSON,因为它会自动为您解码:

https://angular.io/guide/http#type-checking-the-response

The HttpClient.get() method parsed the JSON server response into the anonymous Object type. It doesn't know what the shape of that object is.

还有 https://angular.io/guide/http#requesting-non-json-data .

关于json - Angular 5,rxjs 映射到 json,类型“对象”上不存在 'json',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49884348/

相关文章:

javascript - JSON.parse、JS 类型转换和 revivers

php - 使用 Symfony 2 序列化器对对象中的嵌套结构进行非规范化

html - ionic2 如何获取其他页面的输入数据

angular - 将值动态分配给 Angular 2 中的 SASS mixin - Ionic 2

javascript - Google map 未显示在 Modal Ionic 中

android - 空白页 Ionic 4 android 5.1

java - 在 Java 中将网页响应解析为 Json

javascript - 在元素 json 数组上循环

javascript - Angular (Angular 4) RouterLinkActive 用于菜单和子菜单配置

angular - 等待两个订阅完成而不嵌套