【问题标题】:how to parse the answer (array) you get in the API (GET) query in flutter如何解析你在颤动的API(GET)查询中得到的答案(数组)
【发布时间】:2021-12-29 13:21:55
【问题描述】:

我正在查询进入颤振,我正在返回数组(数组中的一个对象)。我想解析这个答案并将数组对象中的数据分配给变量。但我做不到。这是我的要求:

  Future<Null>   example(
      String? task_id
      ) async {

    HttpClient client = new HttpClient();
    client.badCertificateCallback =
    ((X509Certificate cert, String host, int port) => true);

    final String url = urlVar "?id="+  '$task_id';

    final request = await client
        .getUrl(Uri.parse(url))
        .timeout(Duration(seconds: 5));


    HttpClientResponse response = await request.close();

    var responseBody = await response.transform(utf8.decoder).join();

    Map jsonResponse = json.decode(responseBody);

    print(jsonResponse);


  }

我的答案 json

[
  {
    "id": "d290111e-6c54-4b01-90e6-d701748f0851",
    "name": "Parse example",
   }
]

我想解析这个答案并将其分配给我的变量。当答案在对象中时,我这样做了,我这样做了。但是没有数组,我将不胜感激)

    var responseBody = await response.transform(utf8.decoder).join();

    Map jsonResponse = json.decode(responseBody);

    print(jsonResponse);
    if (response.statusCode == 200) {
      global.taskId = jsonResponse['id'] ;
      global.taskName = jsonResponse['name'];

    }

【问题讨论】:

  • 尝试重新检查json.decode结果,是Map还是List
  • @cahyo my json [ { "id": "d290111e-6c54-4b01-90e6-d701748f0851", "name": "解析示例", } ]
  • 这里可能已经回答了stackoverflow.com/questions/53001839/…

标签: flutter dart


【解决方案1】:

试试这个

if (response.statusCode == 200) {
  global.taskId = jsonResponse[0]['id'] ;
  global.taskName = jsonResponse[0]['name'];

}

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多