【问题标题】:Json Decoding Failed?Json解码失败?
【发布时间】:2020-01-18 01:02:53
【问题描述】:

https://picsum.photos/v2/list?page=1&limit=50 我正在使用这个 Json 文件,但是当我尝试解码时它显示为 null。

我试过了

'''

List imgData;

final String url = 'https://picsum.photos/v2/list?page=1&limit=50';
Future getData() async {
var response = await http
    .get(Uri.encodeFull(url), headers: {'Accept': 'application/json'});

List data = jsonDecode(response.body)['results'];
setState(() {
  imgData = data;
});
}

@override
void initState() {
super.initState();
this.getData();
}

'''

【问题讨论】:

    标签: json flutter flutter-layout flutter-dependencies


    【解决方案1】:

    您可以使用在线免费提供的JSON to dart tool

    将您的 JSON 粘贴到左侧面板并从右上角选择 dart 语言,

    您将获得 dart 类代码,您可以在其中使用 .toMap() 和 .toJson() 等方法,

    而且速度非常快,人为错误更少。

    这对于庞大的 JSON 数据非常有用。

    您将从这个工具中为您的数据获得这样的课程。

    // To parse this JSON data, do
    //
    //     final data = dataFromJson(jsonString);
    
    import 'dart:convert';
    
    List<Data> dataFromJson(String str) => List<Data>.from(json.decode(str).map((x) => Data.fromMap(x)));
    
    String dataToJson(List<Data> data) => json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
    
    class Data {
        String id;
        String author;
        int width;
        int height;
        String url;
        String downloadUrl;
    
        Data({
            this.id,
            this.author,
            this.width,
            this.height,
            this.url,
            this.downloadUrl,
        });
    
        factory Data.fromMap(Map<String, dynamic> json) => Data(
            id: json["id"],
            author: json["author"],
            width: json["width"],
            height: json["height"],
            url: json["url"],
            downloadUrl: json["download_url"],
        );
    
        Map<String, dynamic> toMap() => {
            "id": id,
            "author": author,
            "width": width,
            "height": height,
            "url": url,
            "download_url": downloadUrl,
        };
    }
    

    之后就可以使用dataFromJson方法了。

    这可以是您代码的完整实现。

    import 'dart:convert';
    
    List imgData;
    
    final String url = 'https://picsum.photos/v2/list?page=1&limit=50';
    Future getData() async {
      var response = await http
          .get(Uri.encodeFull(url), headers: {'Accept': 'application/json'});
    
      List data = dataFromJson(response.body);
      setState(() {
        imgData = data;
      });
    }
    
    @override
    void initState() {
      super.initState();
      this.getData();
    }
    
    
    List<Data> dataFromJson(String str) => List<Data>.from(json.decode(str).map((x) => Data.fromMap(x)));
    
    String dataToJson(List<Data> data) => json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
    
    class Data {
      String id;
      String author;
      int width;
      int height;
      String url;
      String downloadUrl;
    
      Data({
        this.id,
        this.author,
        this.width,
        this.height,
        this.url,
        this.downloadUrl,
      });
    
      factory Data.fromMap(Map<String, dynamic> json) => Data(
        id: json["id"],
        author: json["author"],
        width: json["width"],
        height: json["height"],
        url: json["url"],
        downloadUrl: json["download_url"],
      );
    
      Map<String, dynamic> toMap() => {
        "id": id,
        "author": author,
        "width": width,
        "height": height,
        "url": url,
        "download_url": downloadUrl,
      };
    }
    

    【讨论】:

    • 您好,您能帮我看看您所说的“您可以使用 .toMap() 和 .toJson() 之类的方法”,有什么指南吗?初学者通过这个找到出路
    • 是的,我已经编辑了答案。如果您还有任何问题,请随时发表评论。
    【解决方案2】:

    尝试在下面替换

    List data = jsonDecode(response.body)['results'];
    

    List data = jsonDecode(response.body);
    

    您的 JSON 中没有 results 参数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-27
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 2016-09-24
      • 1970-01-01
      相关资源
      最近更新 更多