Q - iOS - 搜索并使用已解析的JSON数组的特定部分

时间:2016-02-23 11:06:07

标签: ios objective-c arrays json

这是我第一次使用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吗?欢迎任何想法!

由于

2 个答案:

答案 0 :(得分:1)

1)根据给定的文档,时间是GMT格式,因此您需要将IST中的时间转换为比较 这是转换时间的代码。我不知道你在Indian Standard Time

转换它的时区
     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"]

答案 1 :(得分:0)

使用以下代码

  double comparingdt = 1456308000;

__block NSString *tempday, *weathericon;

[weatherList enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSDictionary  *weather, NSUInteger idx, BOOL * _Nonnull stop) {

    *stop = ([weather[@"dt"] doubleValue] == comparingdt);

    if (*stop)
    {
        tempday = weather[@"temp"][@"day"];
        weathericon = [weather[@"weather"] firstObject][@"icon"];
    }

}];