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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用下面的类从web服务中动态读取下面给出的JSON数据,并为收到的数据创建水平列表视图。相关的知识,希望对你有一定的参考价值。

我想在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;
  }
}

答案
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)); 

以上是关于如何使用下面的类从web服务中动态读取下面给出的JSON数据,并为收到的数据创建水平列表视图。的主要内容,如果未能解决你的问题,请参考以下文章

如何在不重新编译的情况下在 .NET 中动态切换 Web 服务地址?

如何使用创建的自定义 IBDesignable 类从情节提要中更改按钮的角半径

如何使用默认类从 JSON 登录尝试中读取用户凭据?

如何使用与 NewtonSoft (JSON.Net) 组件中的 JSON 匹配的 Swift 类从/向 JSON 读取/写入对象数组?

.net 如何读取出web某一目录下面的所有图片 并显示在网页中

读取 OBIEE 实例配置文件