How to get data from a nested JSON object in flutter?

To get data from a nested JSON object in Flutter, you can use the dart:convert library to convert the JSON data into a Map, then use the key-value pairs to access the nested values.

Here is an example:

Suppose you have the following JSON data:

  "name": "John",
  "age": 20,
  "address": {
    "street": "103 Main St",
    "city": "Anytown",
    "state": "CA"

You can convert it into a Map in Flutter like this:

import 'dart:convert';

// assume jsonData is the JSON string above
Map<String, dynamic> data = json.decode(jsonData);

To access the nested value of “street” in the “address” object, you can use the following code:

String street = data["address"]["street"];

Similarly, to access other nested values, you can use the same approach by chaining the keys with square brackets.

READ ALSO ...  How to set Flutter Orientation Portrait Or Landscape Only

Leave a Comment