根据今天的日期获取 JSON 文件的内容
Get contents of JSON file based on Todays Date
我有一个应用程序,每天都会显示不同的内容基于那个日期。
即1月1日有picture1
,1月2日有picture2
,以此类推
我已经在本地获取 Days.json 文件中的所有 数据,并且我知道如何提取 当前日期 ,但我不知道如何获取特定日期的数据。
例如:拉入当天的图文,显示在屏幕上
有什么想法吗? post 是否需要任何额外的代码!
**Days.json**
片段:
{
"days": [
{
"day": "Jan 1",
"topic": "1 Endurance",
"image": "picture1"
},
{
"day": "Jan 2",
"topic": "2 Endurance"
"image": "picture2"
},
{
"day": "Apr 18",
"topic": "3 Endurance",
"image": "picture3"
}
]
}
**ViewController.m**
//Get todays date to match date format in Days.json
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateToday = [formatter stringFromDate:[NSDate date]];
NSLog(@"Date Today: %@", dateToday);
// Remove the year from current date string
NSString *dateTodayShort = [dateToday substringToIndex:[dateToday length]-6];
NSLog(@"Date Today Short: %@", dateTodayShort);
// Get the json file
NSString *JSONFilePath = [[NSBundle mainBundle] pathForResource:@"Days" ofType:@"json"];
NSData *JSONData = [NSData dataWithContentsOfFile:JSONFilePath];
NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];
days = JSONDictionary[@"days"];
NSString *day = [days valueForKey:@"day"];
JSON 被解析为以键 "days" 和对象数组作为值的字典。所以你可以遍历对象来找到你想要的那个。
NSObjet *todayInJson;
for (NSObject *object in days) {
NSString *date_object = [object valueForKey:@"day"];
if [date_object isEqualToString:dateTodayShort] {
todayInJson = object;
//object found..
}
}
我有一个应用程序,每天都会显示不同的内容基于那个日期。
即1月1日有picture1
,1月2日有picture2
,以此类推
我已经在本地获取 Days.json 文件中的所有 数据,并且我知道如何提取 当前日期 ,但我不知道如何获取特定日期的数据。
例如:拉入当天的图文,显示在屏幕上
有什么想法吗? post 是否需要任何额外的代码!
**Days.json**
片段:
{
"days": [
{
"day": "Jan 1",
"topic": "1 Endurance",
"image": "picture1"
},
{
"day": "Jan 2",
"topic": "2 Endurance"
"image": "picture2"
},
{
"day": "Apr 18",
"topic": "3 Endurance",
"image": "picture3"
}
]
}
**ViewController.m**
//Get todays date to match date format in Days.json
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateToday = [formatter stringFromDate:[NSDate date]];
NSLog(@"Date Today: %@", dateToday);
// Remove the year from current date string
NSString *dateTodayShort = [dateToday substringToIndex:[dateToday length]-6];
NSLog(@"Date Today Short: %@", dateTodayShort);
// Get the json file
NSString *JSONFilePath = [[NSBundle mainBundle] pathForResource:@"Days" ofType:@"json"];
NSData *JSONData = [NSData dataWithContentsOfFile:JSONFilePath];
NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:nil];
days = JSONDictionary[@"days"];
NSString *day = [days valueForKey:@"day"];
JSON 被解析为以键 "days" 和对象数组作为值的字典。所以你可以遍历对象来找到你想要的那个。
NSObjet *todayInJson;
for (NSObject *object in days) {
NSString *date_object = [object valueForKey:@"day"];
if [date_object isEqualToString:dateTodayShort] {
todayInJson = object;
//object found..
}
}