Created
November 8, 2020 07:13
-
-
Save ali2236/3cddbbdafbc1632ddd7cfc23a1758af5 to your computer and use it in GitHub Desktop.
convert a json to table calendar events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
void main() { | |
var jsonSource = """ | |
{ | |
"Events": [ | |
{ | |
"id": 1, | |
"event_name": "Cake tasting", | |
"event_photo": "https://dispensaries.s3.amazonaws.com/event_photo/Southern_Cali_Kush_3.jpg", | |
"vendor_name": { | |
"id": 1, | |
"vendor": "Tastey Cakes" | |
}, | |
"refund_available": false, | |
"website": "www.foodcakes.com", | |
"share_count": 0, | |
"check_in_count": 0, | |
"street_address": "123 Fake Street", | |
"city": "Brooklynn", | |
"state": "NY", | |
"zipcode": "12312", | |
"event_tagline": "Taste my cakes", | |
"details": "Cake tasting", | |
"start_date": "2020-11-03", | |
"start_time": "23:33:00", | |
"end_time": "23:33:00", | |
"attendees": [] | |
} | |
] | |
} | |
"""; | |
print(convertJsonToDateMap(jsonSource)); | |
} | |
Map<DateTime, List> convertJsonToDateMap(String jsonSource) { | |
var json = jsonDecode(jsonSource); | |
var jsonEvents = json['Events']; | |
Map<DateTime, List<String>> events = {}; | |
for(var event in jsonEvents){ | |
var date = parseDate(event['start_date']); | |
events.putIfAbsent(date, () => <String>[]); | |
events[date].add(event['event_name']); | |
} | |
return events; | |
} | |
DateTime parseDate(String date) { | |
var parts = date.split('-').map(int.tryParse).toList(); | |
return DateTime(parts[0], parts[1], parts[2]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment