繁体   English   中英

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'

试试这个,但最好使用 model 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

model 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;
  }
}

@Jahidul Islam 你的代码解释得很好,但是如果我在 JSON 文件中的 go 更深,我需要如何更改代码。

因为我需要 go 更深入的操作 -> 消息 -> 文本

{
   "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
     },
  ],
  "regions":[
     {
        "id":38,
        "devices":[
           162
        ]
     },
     {
        "id":39,
        "devices":[
           158,
           130,
           134,
           126
        ]
     },
  ],
  "actions":[
     {
        "id":"place-3",
        "type":"place",
        "activator":"enter",
        "module":"app",
        "hold":600,
        "delay":3,
        "push":true,
        "popup":false,
        "autoCloseAfterSeconds":3,
        "messages":[
           {
              "text":"Herzlich willkommen im Museum",
              "title":"Museum",
              "lang":"DE"
           }
        ],
        "resources":[
           
        ],
        "regions":[
           
        ]
     },
     {
        "id":"proximity-42",
        "type":"proximity",
        "activator":"near",
        "module":"audioguide",
        "margin":2,
        "autoCloseAfterSeconds":2,
        "resources":[
           
        ],
        "documents":[
           35
        ],
        "regions":[
           38
        ]
     },
  ],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM