Reputation: 341
I have a JSON response from my API call. The format is like this.
[
{
"hotelname": "A",
"roomno": "101",
"occupancy": "4"
},
{
"hotelname": "A",
"roomno": "102",
"occupancy": "3"
},
{
"hotelname": "B",
"roomno": "101",
"occupancy": "4"
},
{
"hotelname": "B",
"roomno": "202",
"occupancy": "3"
}
]
I want to write a code where in one dropdown list displays the names of the hotels(A,B,C etc) the other dropdown should display the corresponding roomno.
To achieve this i would like to convert my JSON response to a MAP like the below.
Map<String,String> _hoteldata = {
"101":"A",
"102":"A",
"101":"B",
"202":"B",
};
Upvotes: 0
Views: 1117
Reputation: 34302
First, you parse the json using jsonDecode()
, then create a map from the list, for example, using Map.fromEntries()
:
import 'dart:convert';
var rooms = jsonDecode(json) as List;
var hotelData = Map.fromEntries(
rooms.map((room) => MapEntry(room['roomno'], room['hotelname']))
);
Upvotes: 1