1

我正在为我的应用程序使用Vurig 自定义日历。我需要在日历上标记多个日期,但日期应该是动态的,即来自 json 文件(我正在使用JSONKit)的数据,而不是硬编码的。我怎样才能做到这一点?如何遍历日历以将 json 文件中的日期添加到数组中?

这是我到目前为止的代码:

JSON:

{
    "events": [
                  {
                    "event": {
                        "month": "10",
                        "day": "15",
                        "detail": "Some Detail"
                    }
                  },
                  {
                    "event": {
                        "month": "10",
                        "day": "25",
                        "detail": "Some blah"
                    }
                  }
    ]
}

代码:

-(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;
}
4

1 回答 1

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];
于 2013-10-23T00:45:52.670 回答