我正在为我的应用使用Vurig Custom Calendar。我需要在日历上标记多个日期,但日期应该是动态的,即来自json文件的数据(我正在使用JSONKit
)而不是硬编码。我怎样才能做到这一点?如何循环日历以将日期从json文件添加到数组?
这是我到目前为止的代码:
JSON:
{
"events": [
{
"event": {
"month": "10",
"day": "15",
"detail": "Some Detail"
}
},
{
"event": {
"month": "10",
"day": "25",
"detail": "Some blah"
}
}
]
}
CODE:
-(void)calendarView:(VRGCalendarView *)calendarView switchedToMonth:(int)month targetHeight:(float)targetHeight animated:(BOOL)animated {
NSArray *dates;
id day, month;
JSONDecoder* decoder = [[JSONDecoder alloc] init];
NSData *cdata =[self getJSON];
NSDictionary* listDictionary = [decoder objectWithData:cdata];
NSArray* events =[listDictionary objectForKey:@"events"];
for (NSDictionary *event in events) {
NSDictionary *eventDetails = [event objectForKey:@"event"];
day = [eventDetails objectForKey:@"day"];
month = [eventDetails objectForKey:@"month"];
}
//need help here to mark multiple dates in the calendar if month = currentMonth
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:day]; //data from json file
[components setMonth:month]; //data from json file
NSDate *date1 = [calendar dateFromComponents:components];
dates = [NSArray arrayWithObjects:date1, nil];
[calendarView markDates:dates];
}
- (NSData*) getJSON {
NSError *err = nil;
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *filePath = [bundle pathForResource:@"info" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&err];
return jsonData;
}
答案 0 :(得分:0)
解析json,获取日期,将em添加到数组并标记数组
一个例子(免责声明内联并且没有具体信息)
NSData *jsonData; // comes from somewhere
id parsedJSON = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
id datesAsStringsList = [parsedJSON valueForKey:@"someKeyWhereTheDatesAre"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] initWithDateFormat:@"YYYYMMDD" allowNaturalLanguage:NO];
NSMutableArray *dates = [NSMutableArray array];
for (NSString dateString in datesAsStringsList) {
id newDate = [formatter dateFromString:dateString];
if(newDate)
[dates addObject:newDate];
}
[calendar markDates:dates];