【问题标题】:How to parse this json in flutter?如何在颤动中解析这个json?
【发布时间】:2020-10-28 10:07:59
【问题描述】:

这是我的 Json 我如何在 Flutter 中获取这个 Fetch?我如何为此创建模型? 这是 Json Url http://api.weatherstack.com/current?access_key={MY API}&query=jaipur

【问题讨论】:

    标签: http flutter


    【解决方案1】:

    你可以使用这个项目https://javiercbk.github.io/json_to_dart/或者这个https://app.quicktype.io/并选择Dart语言来帮助你创建基于json数据的模型

    【讨论】:

      【解决方案2】:

      您可以使用 this 自动将 JSON 解析为 Dart 类

        class Weather {
            Request request;
            Location location;
            Current current;
          
            Weather({this.request, this.location, this.current});
          
            Weather.fromJson(Map<String, dynamic> json) {
              request =
                  json['request'] != null ? new Request.fromJson(json['request']) : null;
              location = json['location'] != null
                  ? new Location.fromJson(json['location'])
                  : null;
              current =
                  json['current'] != null ? new Current.fromJson(json['current']) : null;
            }
          
            Map<String, dynamic> toJson() {
              final Map<String, dynamic> data = new Map<String, dynamic>();
              if (this.request != null) {
                data['request'] = this.request.toJson();
              }
              if (this.location != null) {
                data['location'] = this.location.toJson();
              }
              if (this.current != null) {
                data['current'] = this.current.toJson();
              }
              return data;
            }
          }
          
          class Request {
            String type;
            String query;
            String language;
            String unit;
          
            Request({this.type, this.query, this.language, this.unit});
          
            Request.fromJson(Map<String, dynamic> json) {
              type = json['type'];
              query = json['query'];
              language = json['language'];
              unit = json['unit'];
            }
          
            Map<String, dynamic> toJson() {
              final Map<String, dynamic> data = new Map<String, dynamic>();
              data['type'] = this.type;
              data['query'] = this.query;
              data['language'] = this.language;
              data['unit'] = this.unit;
              return data;
            }
          }
          
          class Location {
            String name;
            String country;
            String region;
            String lat;
            String lon;
            String timezoneId;
            String localtime;
            int localtimeEpoch;
            String utcOffset;
          
            Location(
                {this.name,
                this.country,
                this.region,
                this.lat,
                this.lon,
                this.timezoneId,
                this.localtime,
                this.localtimeEpoch,
                this.utcOffset});
          
            Location.fromJson(Map<String, dynamic> json) {
              name = json['name'];
              country = json['country'];
              region = json['region'];
              lat = json['lat'];
              lon = json['lon'];
              timezoneId = json['timezone_id'];
              localtime = json['localtime'];
              localtimeEpoch = json['localtime_epoch'];
              utcOffset = json['utc_offset'];
            }
          
            Map<String, dynamic> toJson() {
              final Map<String, dynamic> data = new Map<String, dynamic>();
              data['name'] = this.name;
              data['country'] = this.country;
              data['region'] = this.region;
              data['lat'] = this.lat;
              data['lon'] = this.lon;
              data['timezone_id'] = this.timezoneId;
              data['localtime'] = this.localtime;
              data['localtime_epoch'] = this.localtimeEpoch;
              data['utc_offset'] = this.utcOffset;
              return data;
            }
          }
          
          class Current {
            String observationTime;
            int temperature;
            int weatherCode;
            List<String> weatherIcons;
            List<String> weatherDescriptions;
            int windSpeed;
            int windDegree;
            String windDir;
            int pressure;
            double precip;
            int humidity;
            int cloudcover;
            int feelslike;
            int uvIndex;
            int visibility;
            String isDay;
          
            Current(
                {this.observationTime,
                this.temperature,
                this.weatherCode,
                this.weatherIcons,
                this.weatherDescriptions,
                this.windSpeed,
                this.windDegree,
                this.windDir,
                this.pressure,
                this.precip,
                this.humidity,
                this.cloudcover,
                this.feelslike,
                this.uvIndex,
                this.visibility,
                this.isDay});
          
            Current.fromJson(Map<String, dynamic> json) {
              observationTime = json['observation_time'];
              temperature = json['temperature'];
              weatherCode = json['weather_code'];
              weatherIcons = json['weather_icons'].cast<String>();
              weatherDescriptions = json['weather_descriptions'].cast<String>();
              windSpeed = json['wind_speed'];
              windDegree = json['wind_degree'];
              windDir = json['wind_dir'];
              pressure = json['pressure'];
              precip = json['precip'];
              humidity = json['humidity'];
              cloudcover = json['cloudcover'];
              feelslike = json['feelslike'];
              uvIndex = json['uv_index'];
              visibility = json['visibility'];
              isDay = json['is_day'];
            }
          
            Map<String, dynamic> toJson() {
              final Map<String, dynamic> data = new Map<String, dynamic>();
              data['observation_time'] = this.observationTime;
              data['temperature'] = this.temperature;
              data['weather_code'] = this.weatherCode;
              data['weather_icons'] = this.weatherIcons;
              data['weather_descriptions'] = this.weatherDescriptions;
              data['wind_speed'] = this.windSpeed;
              data['wind_degree'] = this.windDegree;
              data['wind_dir'] = this.windDir;
              data['pressure'] = this.pressure;
              data['precip'] = this.precip;
              data['humidity'] = this.humidity;
              data['cloudcover'] = this.cloudcover;
              data['feelslike'] = this.feelslike;
              data['uv_index'] = this.uvIndex;
              data['visibility'] = this.visibility;
              data['is_day'] = this.isDay;
              return data;
            }
          }
      

      通过Weather.fromJson(json);将json转换为飞镖

      【讨论】:

      • 我该如何申请??发短信?
      猜你喜欢
      • 2020-08-27
      • 2021-08-18
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 2021-08-23
      • 2020-03-01
      • 2020-08-30
      • 2019-11-08
      相关资源
      最近更新 更多