0

Hello guys i want to know how can i get the data from devices in this JSON file below.

My 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
     },

My Method:

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"]);
  });


}

i tried with extractedData["data"] and something else but it doesnt work.

at this actual code i get this Error

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'
Eren G
  • 43
  • 8
  • If you get data from API refer my answer [here](https://stackoverflow.com/a/68709502/13997210) or [here](https://stackoverflow.com/a/68533647/13997210) or [here](https://stackoverflow.com/a/68594656/13997210) hope it's helpful to you – Ravindra S. Patil Jan 07 '22 at 12:51

1 Answers1

0

Try with this, but it would good to use 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

Complete code with the 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
  • 11,435
  • 3
  • 17
  • 38