如何使用下面的类从web服务中动态读取下面给出的JSON数据,并为收到的数据创建水平列表视图。

问题描述 投票:0回答:1

我想在flutter中动态地从web服务中读取JSON数据,我想从web服务中获取数据,并使用Future Builder创建水平列表视图。我获取了json响应,但不能根据提到的类进行读取。

 {
"count": 4,
"result": [
    {
        "deviceId": "kfr",
        "location": {
            "iconId": 1,
            "id": 3,
            "name": "ram's room",
            "timestamp": 1586927632,
            "userId": 1
        },
        "name": "Kitchen Fridge",
        "timestamp": 1587481358
    },
    {
        "deviceId": "kf",
        "location": {
            "iconId": 2,
            "id": 4,
            "name": "ram's room",
            "timestamp": 1586935457,
            "userId": 1
        },
        "name": "Kitchen Fan",
        "timestamp": 1587481484
    },
    {
        "deviceId": "ks",
        "location": {
            "iconId": 3,
            "id": 5,
            "name": "ram's room",
            "timestamp": 1586935457,
            "userId": 1
        },
        "name": "Kitchen Speaker",
        "timestamp": 1587481554
    },
    {
        "deviceId": "kth",
        "location": {
            "iconId": 4,
            "id": 6,
            "name": "ram's room",
            "timestamp": 1586935457,
            "userId": 1
        },
        "name": "Kitchen Thermostat",
        "timestamp": 1587481587
    }
]

}

我已经创建了这些类,并希望动态地读取Json数据。谅谅

  class DeviceDetails {
  int count;
  List<DeviceResult> result;

  DeviceDetails({this.count, this.result});

  DeviceDetails.fromJson(Map<String, dynamic> json) {
    count = json['count'];
    if (json['result'] != null) {
      result = new List<DeviceResult>();
      json['result'].forEach((v) {
        result.add(new DeviceResult.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['count'] = this.count;
    if (this.result != null) {
      data['result'] = this.result.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class DeviceResult {
  String deviceId;
  DeviceLocation location;
  String name;
  int timestamp;

  DeviceResult({this.deviceId, this.location, this.name, this.timestamp});

  DeviceResult.fromJson(Map<String, dynamic> json) {
    deviceId = json['deviceId'];
    location = json['location'] != null
        ? new DeviceLocation.fromJson(json['location'])
        : null;
    name = json['name'];
    timestamp = json['timestamp'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['deviceId'] = this.deviceId;
    if (this.location != null) {
      data['location'] = this.location.toJson();
    }
    data['name'] = this.name;
    data['timestamp'] = this.timestamp;
    return data;
  }
}

class DeviceLocation {
  int iconId;
  int id;
  String name;
  int timestamp;
  int userId;

  DeviceLocation({this.iconId, this.id, this.name, this.timestamp, this.userId});

  DeviceLocation.fromJson(Map<String, dynamic> json) {
    iconId = json['iconId'];
    id = json['id'];
    name = json['name'];
    timestamp = json['timestamp'];
    userId = json['userId'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['iconId'] = this.iconId;
    data['id'] = this.id;
    data['name'] = this.name;
    data['timestamp'] = this.timestamp;
    data['userId'] = this.userId;
    return data;
  }
}

json flutter dart
1个回答
0
投票
import 'dart:convert';

DeviceDetails deviceDetailsFromJson(String str) => DeviceDetails.fromJson(json.decode(str));

String deviceDetailsToJson(DeviceDetails data) => json.encode(data.toJson());

class DeviceDetails {
    int count;
    List<Result> result;

    DeviceDetails({
        this.count,
        this.result,
    });

    factory DeviceDetails.fromJson(Map<String, dynamic> json) => DeviceDetails(
        count: json["count"],
        result: List<Result>.from(json["result"].map((x) => Result.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "count": count,
        "result": List<dynamic>.from(result.map((x) => x.toJson())),
    };
}

class Result {
    String deviceId;
    Location location;
    String name;
    int timestamp;

    Result({
        this.deviceId,
        this.location,
        this.name,
        this.timestamp,
    });

    factory Result.fromJson(Map<String, dynamic> json) => Result(
        deviceId: json["deviceId"],
        location: Location.fromJson(json["location"]),
        name: json["name"],
        timestamp: json["timestamp"],
    );

    Map<String, dynamic> toJson() => {
        "deviceId": deviceId,
        "location": location.toJson(),
        "name": name,
        "timestamp": timestamp,
    };
}

class Location {
    int iconId;
    int id;
    String name;
    int timestamp;
    int userId;

    Location({
        this.iconId,
        this.id,
        this.name,
        this.timestamp,
        this.userId,
    });

    factory Location.fromJson(Map<String, dynamic> json) => Location(
        iconId: json["iconId"],
        id: json["id"],
        name: json["name"],
        timestamp: json["timestamp"],
        userId: json["userId"],
    );

    Map<String, dynamic> toJson() => {
        "iconId": iconId,
        "id": id,
        "name": name,
        "timestamp": timestamp,
        "userId": userId,
    };
} 

读取它

import 'dart:convert' as convert;

Future<DeviceDetails> getData() async{
var res = await http.get('url');
return DeviceDetails.fromJson(
          convert.jsonDecode(res.body)); 
© www.soinside.com 2019 - 2024. All rights reserved.