【问题标题】:Need help to parsing JSON in Flutter在 Flutter 中解析 JSON 需要帮助
【发布时间】:2018-07-07 16:52:36
【问题描述】:

我正在尝试在 Flutter 中从 Internet 获取数据。 但我在 JSON 解析时遇到错误。 谁能告诉我是什么问题?

我正在尝试从此 URL 获取数据

https://swapi.co/api/starships/

示例 JSON

{ “计数”:37, "下一个": "https://swapi.co/api/starships/?page=2", “上一个”:空, “结果”: [ { "name": "执行者", "model": "执行者级星无畏", “制造商”:“Kuat Drive Yards,Fondor Shipyards”, "cost_in_credits": "1143350000", “长度”:“19000”, “最大大气速度”:“不适用”, “船员”:“279144”, “乘客”:“38000”, "cargo_capacity": "250000000", “消耗品”:“6 年”, “hyperdrive_rating”:“2.0”, “MGLT”:“40”, "starship_class": "星际无畏", “飞行员”:[], “电影”:[ “https://swapi.co/api/films/2/”, “https://swapi.co/api/films/3/” ], “创建”:“2014-12-15T12:31:42.547000Z”, “已编辑”:“2017-04-19T10:56:06.685592Z”, “网址”:“https://swapi.co/api/starships/15/” }, ] }

模型类

class RestModel {
    final String name;
    final String model;
    final String manufacturer;
    final String cost_in_credits;
    final String length;
    final String max_atmosphering_speed;
    final String crew;
    final String passengers;
    final String cargo_capacity;
    final String consumables;
    final String hyperdrive_rating;
    final String MGLT;
    final String starship_class;
    final List films;
    final String pilots;
    final String created;
    final String edited;
    final String url;

    RestModel(
        {this.name,
        this.model,
        this.manufacturer,
        this.cost_in_credits,
        this.length,
        this.max_atmosphering_speed,
        this.crew,
        this.passengers,
        this.cargo_capacity,
        this.consumables,
        this.hyperdrive_rating,
        this.MGLT,
        this.starship_class,
        this.films,
        this.pilots,
        this.created,
        this.edited,
        this.url});

    factory RestModel.fromJson(Map<String, dynamic> json) {
        return RestModel(
            name: json["name"],
            model: json["model"],
            manufacturer: json["manufacturer"],
            cost_in_credits: json["cost_in_credits"],
            max_atmosphering_speed: json["max_atmosphering_speed"],
            crew: json["crew"],
            passengers: json["passengers"],
            cargo_capacity: json["cargo_capacity"],
            consumables: json["consumables"],
            hyperdrive_rating: json["hyperdrive_rating"],
            MGLT: json["MGLT"],
            starship_class: json["starship_class"],
            films: json["flims"],
            pilots: json["pilots"],
            created: json["created"],
            edited: json["edited"],
            url: json["url"],
        );
    }
}

Flutter 代码是:

final link = "https://swapi.co/api/starships/";

List<RestModel> list;

Future getData() async {
    var res = await http
    .get(Uri.encodeFull(link), headers: {"Accept":"application/json"});

    if (res.statusCode == 200) {
        var data = json.decode(res.body);
        var rest = data["results"];
        for (var model in rest) {
            list.add(RestModel.fromJson(model));
        }
        print("List Size: ${list.length}");
    }  
}

主要问题是当它尝试从 JSON 填充数据时。

RestModel.fromJson(model)

所以我必须改变什么来解决这个问题。

【问题讨论】:

  • pilots 在您的模型中被声明为字符串,而它的列表在 json 中。
  • 还是一样的结果,
  • 我在 dart 中尝试这段代码,void main() async{ final link = "https://swapi.co/api/starships/"; List&lt;RestModel&gt; list; var res = await http .get(Uri.encodeFull(link), headers: {"Accept": "application/json"}); if (res.statusCode == 200) { var data = json.decode(res.body); var rest = data["results"]; for (var json in rest) { list.add(RestModel.fromJson(json)); } print("List Size: ${list.length}"); } },它给出了这个错误 NoSuchMethodError: No static method 'fromJson' declaration in class 'RestModel'。

标签: dart flutter


【解决方案1】:

尝试将数据“results”转换为List,如下所示:

var rest = data["results"] as List; 

更新

现在我们知道了错误日志:“No static method 'fromJson' declaration in class 'RestModel'”

这是因为你在这一行使用了静态方法:

list.add(RestModel.fromJson(model));

您必须更改调用才能使用工厂构造函数,如下所示:

list.add(new RestModel.fromJson(model));

【讨论】:

  • 列表不是问题,主要问题在这一行list.add(RestModel.fromJson(json));并显示异常list.add(RestModel.fromJson(json));
  • 在“RestModel”类中没有声明静态方法“fromJson”
  • new 关键字是可选的,因此现在您的更新答案是正确的。此外,根据@ShudiptoTrafder 的错误,您的原始答案根本不可能是正确的。
  • Dart 2.0 于 2018 年 8 月发布,请查看我的回答日期
猜你喜欢
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
相关资源
最近更新 更多