0

我想根据当天的时间从我的 google places json 响应中获取关闭时间。这行代码为我提供了一周中当前日期的 0-6 值。

NSString *dayOfWeek = [myFormatter stringFromDate:today];

我有一个 NSArray 来存储谷歌位置响应的结果。

NSArray* periods= [[place objectForKey:@"opening_hours"]objectForKey:@"periods"];

period 数组中的 json 响应如下所示。

Period RESULT (
        {
        close =         {
            day = 0;
            time = 2300;
        };
        open =         {
            day = 0;
            time = 1100;
        };
    },
        {
        close =         {
            day = 1;
            time = 2300;
        };
        open =         {
            day = 1;
            time = 1100;
        };
    },
        {
        close =         {
            day = 2;
            time = 2300;
        };
        open =         {
            day = 2;
            time = 1100;
        };
    },
        {
        close =         {
            day = 3;
            time = 2300;
        };
        open =         {
            day = 3;
            time = 1100;
        };
    },
        {
        close =         {
            day = 4;
            time = 2300;
        };
        open =         {
            day = 4;
            time = 1100;
        };
    },
        {
        close =         {
            day = 5;
            time = 2300;
        };
        open =         {
            day = 5;
            time = 1100;
        };
    },
        {
        close =         {
            day = 6;
            time = 2300;
        };
        open =         {
            day = 6;
            time = 1100;
        };
    }
)

如何使用我的 dayOfWeek 值来检查 period 数组的正确 close.time?

4

2 回答 2

1
for( NSDictionary *d in periods ){
  NSInteger day = [[[d objectForKey:@"close"] objectForKey:@"day"] integerValue];

  if( day == [dayOfWeek integerValue] ){
    return [[[d objectForKey:@"close"] objectForKey:@"time"] integerValue];
  }
}
于 2013-08-23T16:39:51.943 回答
-1

您可以这样做的一种方法是利用NSPredicateKey-Value Coding

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"close.day = %@", dayOfWeek];
NSArray *output = [periods filteredArrayUsingPredicate:predicate];
NSNumber *closingTime = [output.lastObject valueForKeyPath:@"close.time"];
于 2013-08-23T14:40:44.097 回答