首页 文章

Flutter Openweather API 调用

提问于
浏览
1

我想从 Openweather API 解析 JSON。

Openweather URL 是:http://samples.openweathermap.org/data/2.5/weather?q=London8,英国和 appid=b6907d289e10d714a6e88b30761fae22

我正在努力寻找解析 JSON 数据的最简单方法,以获得“描述”和“临时”值。

有人有什么想法吗?

1 回答

  • 0

    发出 HTTP 请求,提取数据,创建仅创建提取数据的地图:

    import 'dart:io';
    import 'dart:convert';
    
    Future<Map> getData() async {
      var httpClient = new HttpClient();
      var request = await httpClient.getUrl(url);
      var response = await request.close();
      var data = json.decode(response);
      var description = data['weather']['description'];
      var temp = data['main']['temp'];
      return { 'description': description, 'temp': temp }; 
    }
    
    main() async {
      print(await getData());
    }
    

相关问题