这是我第一次使用 JSON。在我的应用程序中,我正在从“Open Weather API”获取一些数据。我得到了一些数组,其中有很多条目,我需要搜索特定日期的数组并从中返回 temp.day 和 weather.icon。到目前为止,我成功地获取了数据。据我所知,我应该在其中使用一些带有 IF 的 FOR 循环,但我正在努力解决如何在 Objective C 中执行此操作。
这是我的获取方法:
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* weatherList = [json objectForKey:@"list"];
NSLog(@"everything %@", weatherList ); //3
}
和来自 NSLog 的部分数组:
{
clouds = 92;
deg = 265;
dt = 1456221600;
humidity = 100;
pressure = "1007.96";
rain = "0.24";
speed = "5.12";
temp = {
day = "4.11";
eve = "4.62";
max = "4.87";
min = "2.78";
morn = "4.11";
night = "2.78";
};
weather = (
{
description = "light rain";
icon = 10d;
id = 500;
main = Rain;
}
);
},
{
clouds = 88;
deg = 268;
dt = 1456308000;
humidity = 98;
pressure = "1012.04";
rain = "1.31";
speed = "6.35";
temp = {
day = "3.57";
eve = "3.74";
max = "3.74";
min = "2.38";
morn = "2.53";
night = "2.38";
};
weather = (
{
description = "light rain";
icon = 10d;
id = 500;
main = Rain;
}
);
},
{
clouds = 20;
deg = 243;
dt = 1456394400;
humidity = 100;
pressure = "1015.42";
snow = "0.25";
speed = "5.08";
temp = {
day = "2.95";
eve = "4.09";
max = "4.54";
min = "-0.65";
morn = "1.74";
night = "-0.65";
};
weather = (
{
description = "light snow";
icon = 13d;
id = 600;
main = Snow;
}
);
},
{
clouds = 80;
deg = 273;
dt = 1456480800;
humidity = 100;
pressure = "1016.63";
snow = "0.04";
speed = "2.79";
temp = {
day = "0.2";
eve = "2.79";
max = "2.79";
min = "-3.33";
morn = "-2.7";
night = "-2.42";
};
weather = (
{
description = "light snow";
icon = 13d;
id = 600;
main = Snow;
}
);
},
{
clouds = 87;
deg = 132;
dt = 1456567200;
humidity = 0;
pressure = "1007.07";
rain = "0.22";
snow = "0.03";
speed = 5;
temp = {
day = "3.96";
eve = "1.48";
max = "3.96";
min = "-3.12";
morn = "-3.12";
night = "-1.59";
};
weather = (
{
description = "clear sky";
icon = 01d;
id = 800;
main = Clear;
}
);
}
什么是好的做法?我是否获取了足够大的 JSON 部分?
我也应该使用 dome NSDictionary 吗?欢迎任何想法!
谢谢
最佳答案
1) 根据文档,时间采用 GMT 格式,因此您需要将时间转换为 IST
进行比较
这是转换时间的代码。我不知道您的时区,我将其转换为印度标准时间
for ( NSDictionary *obj in weatherList) {
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = @"dd-MM-yyyy";
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
NSDate *inDate = [inDateFormatter dateFromString:[obj objectForKey:@"dt"]];
if ([self isItToday:inDate]) {
NSDictionary * tempDict = [obj objectForKey:@"temp"];
NSDictionary * weatherDict = [[obj objectForKey:@"weather"]objectForKey:0];
NSString * dayTemp = [tempDict objectForKey:@"day"];//only storing for time being you can use where you needed
NSString * icon = [weatherDict objectForKey:@"icon"];//only storing for time being you can use where you needed
}
}
2)现在您需要将其与今天的日期进行比较
+ (BOOL)isItToday:(NSDate *)date {
if (date != nil) {
NSString *givenDateString = [self stringFromDate:date inDateFormat:@"dd-MM-yyyy"];
NSString *toDayString = [self stringFromDate:[NSDate date] inDateFormat:@"dd-MM-yyyy"];
NSDate *givenDate = [self dateFromString:givenDateString inDateFormat:@"dd-MM-yyyy"];
NSDate *toDay = [self dateFromString:toDayString inDateFormat:@"dd-MM-yyyy"];
if ([givenDate isEqualToDate:toDay]) {
return YES;
} else {
return NO;
}
} else {
return NO;
}
}
+ (NSString *)stringFromDate:(NSDate *)date inDateFormat:(NSString *)inDateformat
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:kLocaleIdentifierEN_US_POSIX];
[dateFormatter setDateFormat:inDateformat];//@"dd/MM/yyyy"
return [dateFormatter stringFromDate:date];
}
+ (NSDate *)dateFromString:(NSString *)stringDate inDateFormat:(NSString *)inDateformat
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:kLocaleIdentifierEN_US_POSIX];
[dateFormatter setDateFormat:inDateformat];//@"dd/MM/yyyy"
return [dateFormatter dateFromString:stringDate];
}
还添加此行
#define kLocaleIdentifierEN_US_POSIX [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]
关于ios - Q - iOS - 搜索和使用解析的 JSON 数组的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35575842/