我想动态地从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;
}
}
答案 0 :(得分: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));