【发布时间】: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": "解析示例", } ]