Home > Mobile >  Flutter how can i go deeper in the json file
Flutter how can i go deeper in the json file

Time:01-08

I want to go deeper in the JSON File how what i need to change in the code.

because i need to go deeper in data -> resources-> items-> title

My JSON:

{
   "status":"UPDATE",
   "data":{
  "version":"2",
  "modDate":"2021-12-22T17:33:59 0100",
  "languages":[
     "DE",
     "EN"
  ],
  "mapmarker":[
     
  ],
  "resources":[
     {
        "id":241,
        "type":"image",
        "items":[
           {
              "lang":"DE",
              "title":"Labor",
              "file":"uploads\/",
              "size":1923217,
              "modDate":"2019-02-28T18:07:46 0100"
           },
           {
              "lang":"EN",
              "title":"Faust Labor",
              "file":"uploads\/",
              "size":1923217,
              "modDate":"2019-02-28T18:07:46 0100"
           }
        ]
     },

MY Not working Code actual:

Future<void> getDaten() async {
final response =
      await http.get(Uri.parse("https://goethemuseum.beacon-cms.de/index.php?id=7&version=2"));

  final extractedData = json.decode(response.body) as Map<String, dynamic>;

  var deviceData = extractedData["data"] as Map<String, dynamic>;

  var deviceData2 = deviceData["resources"] as Map<String, dynamic>;

  deviceData["items"].forEach((e)=>print(e["text"]));


}

My existing Error:

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast

I can go only to data -> resources -> items and then i only get Strings like this

[{lang: DE, title: Schmuck, Uhren und Spiele, file: uploads/Spiele.JPG, size: 774265, modDate: 2021-12-13T16:04:25 0100}]

CodePudding user response:

You can do something like this if you want to extract all titles across all items in resources. Please note that we don't need to cast every single value we are using since dynamic means that Dart should just ignore what we are doing with the object and expect any call on dynamic to also be dynamic:

import 'dart:convert';

void main() {
  dynamic jsonObject = jsonDecode(jsonString);

  final titles = [
    for (Map<String, dynamic> resource in jsonObject['data']['resources'])
      for (Map<String, dynamic> item in resource['items'])
        item['title'] as String
  ];

  titles.forEach(print);
  // Labor
  // Faust Labor
}

const jsonString = '''{
  "status": "UPDATE",
  "data": {
    "version": "2",
    "modDate": "2021-12-22T17:33:59 0100",
    "languages": [
      "DE",
      "EN"
    ],
    "mapmarker": [
    ],
    "resources": [
      {
        "id": 241,
        "type": "image",
        "items": [
          {
            "lang": "DE",
            "title": "Labor",
            "file": "uploads\/",
            "size": 1923217,
            "modDate": "2019-02-28T18:07:46 0100"
          },
          {
            "lang": "EN",
            "title": "Faust Labor",
            "file": "uploads\/",
            "size": 1923217,
            "modDate": "2019-02-28T18:07:46 0100"
          }
        ]
      }
    ]
  }
}
''';

CodePudding user response:

In your function below lines

  var deviceData = extractedData["data"] as Map<String, dynamic>;

  var deviceData2 = deviceData["resources"] as Map<String, dynamic>;

Are causing error because in your API extractedData["data"] is returning you a List but you are casting it as Map<String, dynamic> that's why it's throwing an error. To solve this use like this :

  var deviceData = extractedData["data"];

  var deviceData2 = deviceData["resources"];
  •  Tags:  
  • Related