1

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,
      };
Чак Джонс
  • 115
  • 1
  • 1
  • 10
  • If you get data from API so refer my answer [here](https://stackoverflow.com/a/68709502/13997210) or [here](https://stackoverflow.com/a/68533647/13997210) or [here](https://stackoverflow.com/a/68594656/13997210) hope it's helpful to you and solve your problem – Ravindra S. Patil Aug 11 '21 at 16:20

2 Answers2

0

there is a website called json to dart which gives you json model class and everything you need. the only thing you have to do is copy an example of api response and paste it there and it returns the class you want. you can visit this website here.

Benyamin
  • 1,008
  • 11
  • 18
0

Your json parsing should be like this in your case,

    final urlAuth = Uri.parse('http://XXX.XX.XX.XXX/api/usersessions/activeinfo');
    final response = await http
            .get(urlAuth, headers: <String, String>{'authorization': basicAuth});
        
    var jsonData = json.decode(response.body);
    if (response.statusCode == 200) {
       EvrokoStandartPc evrokoStandartPc = EvrokoStandartPc.fromJson(jsonData);
        final listStandart = evrokoStandartPc.result;
        print(listStandart);
    } else {
        throw Exception('Ошибка получения данных');
    }
Bhoomika Chauhan
  • 928
  • 1
  • 9
  • 11