Flutter API 如何使用 http 插件从 JSON 中的设备获取数据
Flutter API how can i get the data from devices in this JSON with http Plugin
大家好,我想知道如何从下面这个 JSON 文件中的设备获取数据。
我的JSON:
{
"status":"UPDATE",
"data":{
"version":"2",
"modDate":"2021-12-22T17:33:59+0100",
"languages":[
"DE",
"EN"
],
"devices":[
{
"id":126,
"uuid":"b9407f30-f5f8-466e-aff9-25556b57fe6d",
"ma":600,
"mi":33815
},
{
"id":129,
"uuid":"b9407f30-f5f8-466e-aff9-25556b57fe6d",
"ma":600,
"mi":28664
},
我的方法:
Future<void> getDaten() async {
final response =
await http.get(Uri.parse("https://blablabla.de/index.php?id=7&version=2"));
final extractedData = json.decode(response.body) as Map<String, dynamic>;
print(extractedData);
extractedData.forEach((id, data) {
print(id);
print(data["devices"]);
});
}
我尝试使用 extractedData["data"] 和其他东西,但它不起作用。
在这个实际代码中我得到了这个错误
E/flutter (19705): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
试试这个,但最好使用模型 class
final extractedData = json.decode(response.body) as Map<String, dynamic>;
var deviceData = extractedData["data"] as Map<String, dynamic>;
deviceData["devices"].forEach((e)=>print(e["id"])); // device id 126 129
用模型完成代码class
import 'dart:math';
void main() {
final extractedData = {
"status": "UPDATE",
"data": {
"version": "2",
"modDate": "2021-12-22T17:33:59+0100",
"languages": ["DE", "EN"],
"devices": [
{
"id": 126,
"uuid": "b9407f30-f5f8-466e-aff9-25556b57fe6d",
"ma": 600,
"mi": 33815
},
{
"id": 129,
"uuid": "b9407f30-f5f8-466e-aff9-25556b57fe6d",
"ma": 600,
"mi": 28664
},
]
}
};
ItemModel itemModel= ItemModel.fromJson(extractedData);
List<Devices>? devices = itemModel.data?.devices;
print(devices);
}
class ItemModel {
String? status;
Data? data;
ItemModel({this.status, this.data});
ItemModel.fromJson(Map<String, dynamic> json) {
status = json['status'];
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
if (this.data != null) {
data['data'] = this.data!.toJson();
}
return data;
}
}
class Data {
String? version;
String? modDate;
List<String>? languages;
List<Devices>? devices;
Data({this.version, this.modDate, this.languages, this.devices});
Data.fromJson(Map<String, dynamic> json) {
version = json['version'];
modDate = json['modDate'];
languages = json['languages'].cast<String>();
if (json['devices'] != null) {
devices = <Devices>[];
json['devices'].forEach((v) {
devices!.add(new Devices.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['version'] = this.version;
data['modDate'] = this.modDate;
data['languages'] = this.languages;
if (this.devices != null) {
data['devices'] = this.devices!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Devices {
int? id;
String? uuid;
int? ma;
int? mi;
Devices({this.id, this.uuid, this.ma, this.mi});
Devices.fromJson(Map<String, dynamic> json) {
id = json['id'];
uuid = json['uuid'];
ma = json['ma'];
mi = json['mi'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['uuid'] = this.uuid;
data['ma'] = this.ma;
data['mi'] = this.mi;
return data;
}
}
大家好,我想知道如何从下面这个 JSON 文件中的设备获取数据。
我的JSON:
{
"status":"UPDATE",
"data":{
"version":"2",
"modDate":"2021-12-22T17:33:59+0100",
"languages":[
"DE",
"EN"
],
"devices":[
{
"id":126,
"uuid":"b9407f30-f5f8-466e-aff9-25556b57fe6d",
"ma":600,
"mi":33815
},
{
"id":129,
"uuid":"b9407f30-f5f8-466e-aff9-25556b57fe6d",
"ma":600,
"mi":28664
},
我的方法:
Future<void> getDaten() async {
final response =
await http.get(Uri.parse("https://blablabla.de/index.php?id=7&version=2"));
final extractedData = json.decode(response.body) as Map<String, dynamic>;
print(extractedData);
extractedData.forEach((id, data) {
print(id);
print(data["devices"]);
});
}
我尝试使用 extractedData["data"] 和其他东西,但它不起作用。
在这个实际代码中我得到了这个错误
E/flutter (19705): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
试试这个,但最好使用模型 class
final extractedData = json.decode(response.body) as Map<String, dynamic>;
var deviceData = extractedData["data"] as Map<String, dynamic>;
deviceData["devices"].forEach((e)=>print(e["id"])); // device id 126 129
用模型完成代码class
import 'dart:math';
void main() {
final extractedData = {
"status": "UPDATE",
"data": {
"version": "2",
"modDate": "2021-12-22T17:33:59+0100",
"languages": ["DE", "EN"],
"devices": [
{
"id": 126,
"uuid": "b9407f30-f5f8-466e-aff9-25556b57fe6d",
"ma": 600,
"mi": 33815
},
{
"id": 129,
"uuid": "b9407f30-f5f8-466e-aff9-25556b57fe6d",
"ma": 600,
"mi": 28664
},
]
}
};
ItemModel itemModel= ItemModel.fromJson(extractedData);
List<Devices>? devices = itemModel.data?.devices;
print(devices);
}
class ItemModel {
String? status;
Data? data;
ItemModel({this.status, this.data});
ItemModel.fromJson(Map<String, dynamic> json) {
status = json['status'];
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
if (this.data != null) {
data['data'] = this.data!.toJson();
}
return data;
}
}
class Data {
String? version;
String? modDate;
List<String>? languages;
List<Devices>? devices;
Data({this.version, this.modDate, this.languages, this.devices});
Data.fromJson(Map<String, dynamic> json) {
version = json['version'];
modDate = json['modDate'];
languages = json['languages'].cast<String>();
if (json['devices'] != null) {
devices = <Devices>[];
json['devices'].forEach((v) {
devices!.add(new Devices.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['version'] = this.version;
data['modDate'] = this.modDate;
data['languages'] = this.languages;
if (this.devices != null) {
data['devices'] = this.devices!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Devices {
int? id;
String? uuid;
int? ma;
int? mi;
Devices({this.id, this.uuid, this.ma, this.mi});
Devices.fromJson(Map<String, dynamic> json) {
id = json['id'];
uuid = json['uuid'];
ma = json['ma'];
mi = json['mi'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['uuid'] = this.uuid;
data['ma'] = this.ma;
data['mi'] = this.mi;
return data;
}
}