【发布时间】:2021-08-23 17:37:03
【问题描述】:
我正在尝试获取来自 Http 库的 JSON 数据。我只想向用户显示第一个对象的“alert_description”值。我如何才能访问此属性?
我的 API 响应:
{
"code": 0,
"message": " success",
"data": {
"data": {
"current_page": 1,
"data": [
{
"id": 62,
"user_id": 53,
"boxIdentifiant": 1924589682265245,
"boxName": "Box Sfax",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression",
"alert_level": "warning"
},
{
"id": 61,
"user_id": 53,
"boxIdentifiant": 1924589682265243,
"boxName": "Box Tunis",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression Roux",
"alert_level": "info"
},
{
"id": 58,
"user_id": 53,
"boxIdentifiant": 1924589682265244,
"boxName": "Box Office",
"alert_date": "2021-05-30",
"alert_time": "09:40",
"alert_description": "Panne Pression Roux",
"alert_level": "warning"
},
我的代码:
var response =
await http.get(Uri.parse(ApiUtil.GET_ALERT), headers: headers);
print("here================");
// print(response);
var data = json.decode(response.body);
print(data['data']['data']['data']);
if (data['status'] == 200) {
showNotification(data['message'], flp);
} else {
print("no message");
}
return Future.value(true);
});
}
【问题讨论】:
-
我强烈推荐使用 json.decode()。 stackoverflow.com/questions/51601519/…