skip to Main Content

I’m trying to map a variable to receive data from my API through jsonDecode(response.body)

Here’s how I try to map, but I don’t know if this is correct or not.
The data from API.

    {
    "days": {
        "region1": [
            {
                "1": "2",
                "2": "Monday",
                "3": "25",
                "4": "10"
            },
            {
                "1": "1",
                "2": "Tuesday",
                "3": "21",
                "4": "12"
            },
       ]
   }
}

I want to have a variable to receive that data above.

  final Map<String, dynamic> listData = jsonDecode(response.body);

    for (final item in listData.entries) {
         print(item.days);
     }

How can I print out the days? or region1? how can I access to value Monday
I try this print(item.days) but it not work.

2

Answers


  1. First, to access the "days" key in your JSON data, you can directly use listData[‘days’]. You don’t need to iterate through the entries for this specific case.

    Second, "days" contains a map where the keys are strings like "region1," and the values are lists of maps. You can access the "region1" data by using listData[‘days’][‘region1’].

    You can refer this one:

    final Map<String, dynamic> responseData = jsonDecode(response.body);
    final Map<String, dynamic> daysData = responseData['days'];
    final List<dynamic> region1Data = daysData['region1'];
    
    for (final item in region1Data) {
      final String dayOfWeek = item['2'];
      final String value1 = item['1'];
      final String value3 = item['3'];
      final String value4 = item['4'];
    
      print('Day: $dayOfWeek, Value1: $value1, Value3: $value3, Value4: $value4');
    }
    
    Login or Signup to reply.
  2. To get the map attribute, you can use the property name inside the square bracket.

    final test = {"fruit": "apple"}
    print(test['fruit');  => prints "apple"
    

    In your code, you can do the same.

    Example:

    import "dart:convert";
    
    void main() {
      final data =    {
        "days": {
            "region1": [
                {
                    "1": "2",
                    "2": "Monday",
                    "3": "25",
                    "4": "10"
                },
                {
                    "1": "1",
                    "2": "Tuesday",
                    "3": "21",
                    "4": "12"
                },
           ]
       }
    };
      
      final Map<String, dynamic> listData = jsonDecode(jsonEncode(data));
      print("Listdata: $listData n");
      print("Days: ${listData['days']} n");
      
      final List<dynamic> region1 = listData['days']['region1'];
      
      for(final region in region1){
        print("Day: ${region['2']}");
      }
    }
    

    Output:

    Listdata: {days: {region1: [{1: 2, 2: Monday, 3: 25, 4: 10}, {1: 1, 2: Tuesday, 3: 21, 4: 12}]}} 
    
    Days: {region1: [{1: 2, 2: Monday, 3: 25, 4: 10}, {1: 1, 2: Tuesday, 3: 21, 4: 12}]} 
    
    Day: Monday
    Day: Tuesday
    

    But, I suggest you to convert the Map into Models and access the properties. You can convert the json data to dart model from HERE

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search