【问题标题】:Dart HTTP request to listDart HTTP 请求列表
【发布时间】:2020-11-27 08:34:58
【问题描述】:

我正在尝试将 JSON 数组转换为 Dart 列表。 因此,我首先使用 HTTP 包请求 GET 请求。

代码:

      Future getCurrency() async {
        final String url = 'https://xxx/$currency.php';
        Map<String, String> headers = {
          'Content-Type': 'application/json;charset=UTF-8',
          'Charset': 'utf-8'
        };
        Response response = await get(url, headers: headers);
        data = json.decode(response.body);
        print(data);
      }
    
      void initState() {
        super.initState();
        getCurrency();
}

然后我就卡在这里了。我真的不明白我怎么能做到这一点。 我的 JSON:

{
  "sell": {
    "value": "0",
    "updated": "e",
    "change": {
      "percentage": "e%",
      "sign": "increased"
    }
  },
  "buy": {
    "value": "e",
    "updated": "e",
    "change": {
      "percentage": "%",
      "sign": "increased"
    }
  }
}

【问题讨论】:

  • 如果您能分享您的 data 变量的内容,那就太好了,这样我们就可以帮助您将序列化为一个对象。
  • 您无法将此数据解析为列表。您发送的 JSON 是一个对象,使用 jsonDecode 您会得到一个 Map
  • 我不能将地图转换为列表吗?

标签: flutter http dart


【解决方案1】:

首先,您需要像这样定义一个响应模型:

import 'dart:convert';

ResponseModel responseModelFromJson(String str) => ResponseModel.fromJson(json.decode(str));

String responseModelToJson(ResponseModel data) => json.encode(data.toJson());

class ResponseModel {
    ResponseModel({
        this.sell,
        this.buy,
    });

    Buy sell;
    Buy buy;

    factory ResponseModel.fromJson(Map<String, dynamic> json) => ResponseModel(
        sell: json["sell"] == null ? null : Buy.fromJson(json["sell"]),
        buy: json["buy"] == null ? null : Buy.fromJson(json["buy"]),
    );

    Map<String, dynamic> toJson() => {
        "sell": sell == null ? null : sell.toJson(),
        "buy": buy == null ? null : buy.toJson(),
    };
}

class Buy {
    Buy({
        this.value,
        this.updated,
        this.change,
    });

    String value;
    String updated;
    Change change;

    factory Buy.fromJson(Map<String, dynamic> json) => Buy(
        value: json["value"] == null ? null : json["value"],
        updated: json["updated"] == null ? null : json["updated"],
        change: json["change"] == null ? null : Change.fromJson(json["change"]),
    );

    Map<String, dynamic> toJson() => {
        "value": value == null ? null : value,
        "updated": updated == null ? null : updated,
        "change": change == null ? null : change.toJson(),
    };
}

class Change {
    Change({
        this.percentage,
        this.sign,
    });

    String percentage;
    String sign;

    factory Change.fromJson(Map<String, dynamic> json) => Change(
        percentage: json["percentage"] == null ? null : json["percentage"],
        sign: json["sign"] == null ? null : json["sign"],
    );

    Map<String, dynamic> toJson() => {
        "percentage": percentage == null ? null : percentage,
        "sign": sign == null ? null : sign,
    };
}

然后,您需要在 setState() 方法中设置响应数据。它看起来像这样:

  Future getCurrency() async {
    final String url = 'https://xxx/$currency.php';
    Map<String, String> headers = {
      'Content-Type': 'application/json;charset=UTF-8',
      'Charset': 'utf-8'
    };
    Response response = await get(url, headers: headers);
    setState(() {
      data = json.decode(response.body);
    });

    print(data);
  }

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 2021-04-20
    • 2019-08-14
    相关资源
    最近更新 更多