【问题标题】:How to parse json in flutter如何在颤振中解析json
【发布时间】: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);
  });
}

【问题讨论】:

标签: flutter dart


【解决方案1】:

用于解码这样的 JSON

{
"id":"xx888as88",
"timestamp":"2020-08-18 12:05:40",
"sensors":[
    {
     "name":"Gyroscope",
     "values":[
         {
          "type":"X",
          "value":-3.752716,
          "unit":"r/s"
         },
         {
           "type":"Y",
           "value":1.369709,
           "unit":"r/s"
         },
         {
           "type":"Z",
           "value":-13.085,
           "unit":"r/s"
         }
       ]
    }
  ]
}

你可以这样做:

void setReceivedText(String text) {
    Map<String, dynamic> jsonInput = jsonDecode(text);
    
    _receivedText = 'ID: ' + jsonInput['id'] + '\n';
    _receivedText += 'Date: ' +jsonInput['timestamp']+ '\n';
    _receivedText += 'Device: ' +jsonInput['sensors'][0]['name'] + '\n';
    _receivedText += 'Type: ' +jsonInput['sensors'][0]['values'][0]['type'] + '\n';
    _receivedText += 'Value: ' +jsonInput['sensors'][0]['values'][0]['value'].toString() + '\n';
    _receivedText += 'Type: ' +jsonInput['sensors'][0]['values'][1]['type'] + '\n';
    _receivedText += 'Value: ' +jsonInput['sensors'][0]['values'][1]['value'].toString() + '\n';
    _receivedText += 'Type: ' +jsonInput['sensors'][0]['values'][2]['type'] + '\n';
    _receivedText += 'Value: ' +jsonInput['sensors'][0]['values'][2]['value'].toString();
     _historyText = '\n' + _receivedText;
}

【讨论】:

    【解决方案2】:

    我不知道 Http 库是如何工作的,但是在 Dio 库中你不需要解码任何东西,它非常简单。看看这是否对您有帮助:

    var response = await Dio().post(yourUrl, data: { param1: value1, param2: value2 });
    
    for (var item in response.data['data']['data']['data'])
    {
        print(item['alert_description']);
    }
    

    由于您使用的是GET方法,因此请分别使用Dio().get()queryParameters: 而不是Dio().post()data:

    【讨论】:

    • 我只想向用户显示第一个值作为 Notification 。这向我展示了列表中存在的所有值。 @Linesofcode
    • 试试 response.data['data']['data']['data'][0]
    • 只需确保您的数组长度至少为 1 以避免将来出现问题。遵循@AlperenBaskaya 解决方案。
    • if (data['status'] == 200) { showNotification(dataa[0]['alert_description'], flp); } else { print("没有消息"); } 。控制台上显示的值。我想在屏幕上显示它作为通知。@Linesofcode
    猜你喜欢
    • 2020-03-01
    • 2021-10-27
    • 2019-01-17
    • 2019-09-06
    • 2023-03-22
    • 2021-09-25
    • 2021-02-25
    • 2020-09-12
    • 2023-03-05
    相关资源
    最近更新 更多