【问题标题】:how to parse array to multiple choice QNA flutter如何将数组解析为多项选择 QNA 颤振
【发布时间】:2021-07-29 11:50:58
【问题描述】:

我有一些来自 API 的数据

{
    "status": 200,
    "id": "1",
    "pertanyaan": "Berapa Penghasilan Anda",
    "type": "radio_opt",
    "opsi": "[\"1 juta\",\"2 Juta\",\"3 Juta\",\"4 Juta\",\"> 4 Juta\"]",
    "urutan": "3"
}

如何将此数组解析为 Map ? 任何人都请帮助我:

我的代码:

Map<String, bool> values = {
    'foo': true,
    'bar': false,
  };


if(question.type == "check_opt"){
          // choices = jsonEncode(question.opsi) as List<List<String>>;
          // choices = question.opsi as List<List<String>>;
          values = question.opsi as Map<String, bool>;
        }else if (question.type == "radio_opt"){
          // values = question.opsi as Map<String, dynamic>;
          multi = jsonEncode(question.opsi);
        }

to this

【问题讨论】:

    标签: arrays json flutter array-map


    【解决方案1】:

    不清楚哪些选项应该为真 - 我下面的代码测试前缀 "&gt;"。

    import 'dart:convert';
    
    main(List<String> args) {
      String data = '''{
        "status": 200,
        "id": "1",
        "pertanyaan": "Berapa Penghasilan Anda",
        "type": "radio_opt",
        "opsi": "[\\"1 juta\\",\\"2 Juta\\",\\"3 Juta\\",\\"4 Juta\\",\\"> 4 Juta\\"]",
        "urutan": "3"
    }''';
      Map<String, dynamic> question = json.decode(data);
      if (question["type"] == "radio_opt") {
        var opts = json.decode(question["opsi"]);
        Map<String, bool> map = {for (var opt in opts) opt: opt[0] == '>'};
        print(map);
      }
    }
    

    输出:

    {1 juta: false, 2 Juta: false, 3 Juta: false, 4 Juta: false, > 4 Juta: true}
    

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 2021-06-01
      • 2019-07-27
      • 2021-08-23
      • 2020-03-01
      • 1970-01-01
      • 2020-11-15
      • 2020-10-16
      • 2019-01-17
      相关资源
      最近更新 更多