I want to make a request to get certain data from json and convert this data to a list.
I wrote below code but it doesn't work.
From the request I only need some fields, like username, userId, hostId and hostName.
And them convert this list to another.
Thanks for help!)
This is Json:
"result": [
{
"username": "wwinstton",
"userId": 132,
"span": 2655.294023800003,
"lastLogin": "2021-08-11T08:56:29.0029829",
"lastLogout": "2021-08-10T14:58:11.603158",
"hostId": 19,
"hostName": "S03",
"hostNumber": 3,
"userGroupName": "Standart Members",
"userGroupId": 1,
"hostGroupName": "Standart Computers",
"hostGroupId": 2,
"sessionState": 1,
"slot": 0
}
],
"httpStatusCode": 200
This is my code:
final urlAuth = Uri.parse('http://XXX.XX.XX.XXX/api/usersessions/activeinfo');
final response = await http
.get(urlAuth, headers: <String, String>{'authorization': basicAuth});
final listStandart = json
.decode(response.body)['results']
.map((data) => EvrokoStandartPc.fromJson(data))
.toList();
if (response.statusCode == 200) {
print(listStandart);
} else {
throw Exception('Ошибка получения данных');
}
}
This is class:
class EvrokoStandartPc {
EvrokoStandartPc({
this.result,
this.httpStatusCode,
});
final List<Result>? result;
final int? httpStatusCode;
factory EvrokoStandartPc.fromJson(Map<String, dynamic> json) =>
EvrokoStandartPc(
result:
List<Result>.from(json["result"].map((x) => Result.fromJson(x))),
httpStatusCode: json["httpStatusCode"],
);
Map<String, dynamic> toJson() => {
"result": List<dynamic>.from(result!.map((x) => x.toJson())),
"httpStatusCode": httpStatusCode,
};
}
class Result {
Result({
this.username,
this.userId,
this.hostId,
this.hostName,
});
final String? username;
final int? userId;
final int? hostId;
final String? hostName;
factory Result.fromJson(Map<String, dynamic> json) => Result(
username: json["username"],
userId: json["userId"],
hostId: json["hostId"],
hostName: json["hostName"],
);
Map<String, dynamic> toJson() => {
"username": username,
"userId": userId,
"hostId": hostId,
"hostName": hostName,
};