нужно распарсить сложный json на Dart(Flutter)
Вот json:
[
{
"office": "Пекарня номер 1",
"sensorTable": [
{
"Ok": 1,
"id": 4,
"maxTemp": "-",
"maxTrigerStatus": 0,
"minTemp": "-",
"minTrigerStatus": 0,
"module": 14906218,
"office": "Пекарня номер 1",
"sensor": "DS18B20",
"status": 2,
"tempCur": 22.6,
"tempData": "2021-06-01 12:54:50",
"tempTrigMax": 50,
"tempTrigMin": -50
},
{
"Ok": 1,
"id": 5,
"maxTemp": "-",
"maxTrigerStatus": 0,
"minTemp": "-",
"minTrigerStatus": 0,
"module": 14906218,
"office": "Пекарня номер 1",
"sensor": "temp-1",
"status": 2,
"tempCur": 22.6,
"tempData": "2021-06-01 12:54:50",
"tempTrigMax": 50,
"tempTrigMin": -50
},
{
"Ok": 1,
"id": 6,
"maxTemp": "-",
"maxTrigerStatus": 0,
"minTemp": "-",
"minTrigerStatus": 0,
"module": 14906218,
"office": "Пекарня номер 1",
"sensor": "temp-2",
"status": 2,
"tempCur": 22.1,
"tempData": "2021-06-01 12:54:50",
"tempTrigMax": 0,
"tempTrigMin": 0
}
]
},
{
"office": "Склад",
"sensorTable": [
{
"Ok": 1,
"id": 2,
"maxTemp": "-",
"maxTrigerStatus": 0,
"minTemp": "-",
"minTrigerStatus": 0,
"module": 3427178,
"office": "Склад",
"sensor": "Стеллажи",
"status": 2,
"tempCur": 21.3,
"tempData": "2021-05-12 00:08:36",
"tempTrigMax": 50,
"tempTrigMin": -50
},
{
"Ok": 1,
"id": 3,
"maxTemp": "-",
"maxTrigerStatus": 0,
"minTemp": "-",
"minTrigerStatus": 0,
"module": 3427178,
"office": "Склад",
"sensor": "Узел сортировки",
"status": 2,
"tempCur": -21.1,
"tempData": "2021-05-12 00:08:36",
"tempTrigMax": 50,
"tempTrigMin": -50
}
]
}
]
Код получения данных с сервера:
import 'package:dio/dio.dart';
Dio dio = Dio();
Future getSensorsList() async {
final url = 'http://1c.unduty.ru:3014/api/v1/sensorsList?token=c21bf03d52654a66';
final response = await dio.get(url);
}
Ответы (1 шт):
Автор решения: Maxgmer
→ Ссылка
Сначала добавьте классы-модели, в которые мы положим json данные. Выглядеть они будут так:
Модель с office и sensorTable:
class OfficeSensorsData {
final String office;
final List<SensorData> sensorTable;
OfficeSensorsData(
this.office,
this.sensorTable,
);
static OfficeSensorsData fromJson(Map<String, dynamic> json) {
final sensorTable = List<Map<String, dynamic>>.from(json['sensorTable']);
return OfficeSensorsData(
json['office'],
sensorTable.map((json) => SensorData.fromJson(json)).toList(),
);
}
}
И вторая модель для сущностей из sensorTable:
class SensorData {
final int ok;
final int id;
final String maxTemp;
final int maxTriggerStatus;
final String minTemp;
final int minTriggerStatus;
final int module;
final String office;
final String sensor;
final int status;
final double tempCur;
final String tempDate;
final int tempTrigMax;
final int tempTrigMin;
SensorData(
this.ok,
this.id,
this.maxTemp,
this.maxTriggerStatus,
this.minTemp,
this.minTriggerStatus,
this.module,
this.office,
this.sensor,
this.status,
this.tempCur,
this.tempDate,
this.tempTrigMax,
this.tempTrigMin,
);
static SensorData fromJson(Map<String, dynamic> json) {
return SensorData(
json['Ok'],
json['id'],
json['maxTemp'],
json['maxTrigerStatus'],
json['minTemp'],
json['minTrigerStatus'],
json['module'],
json['office'],
json['sensor'],
json['status'],
json['tempCur'],
json['tempData'],
json['tempTrigMax'],
json['tempTrigMin'],
);
}
}
В моделях, где я указал тип int, возможно нужно будет поменять на double, если может прилететь дробное число.
После добавления моделей, мы можем сделать запрос и сразу его распарсить таким образом:
Future<List<OfficeSensorsData>> getSensorsList() async {
final url = 'http://1c.unduty.ru:3014/api/v1/sensorsList?token=c21bf03d52654a66';
final response = await dio.get(url);
final responseData = List<Map<String, dynamic>>.from(
json.decode(response.data),
);
return responseData.map((json) => OfficeSensorsData.fromJson(json)).toList();
}