0

我有一个提供祈祷时间的 JSON 请求。我如何使用 Objective-C 来确定下一个是哪个?JSON 看起来像这样:

{
    address = "<null>";
    city = "<null>";
    country = UK;
    "country_code" = GB;
    daylight = 1;
    for = daily;
    items =     (
                {
            asr = "5:22 pm";
            "date_for" = "2013-7-1";
            dhuhr = "1:01 pm";
            fajr = "2:15 am";
            isha = "11:47 pm";
            maghrib = "9:24 pm";
            shurooq = "4:39 am";
        }
    );
    latitude = "50.9994081";
    link = "http://muslimsalat.com/UK";
    longitude = "0.5039011";
    "map_image" = "http://maps.google.com/maps/api/staticmap?center=50.9994081,0.5039011&sensor=false&zoom=13&size=300x300";
    "postal_code" = "<null>";
    "prayer_method_name" = "Muslim World League";
    "qibla_direction" = "119.26";
    query = "51.000000,0.500000";
    state = "<null>";
    timezone = 0;
    title = UK;
    "today_weather" =     {
        pressure = 1020;
        temperature = 14;
    };
}

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

-(CLLocationCoordinate2D) getLocation{
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    return coordinate;
}

//class to convert JSON to NSData
- (IBAction)getDataFromJson:(id)sender {
    //get the coords:
    CLLocationCoordinate2D coordinate = [self getLocation];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

    NSLog(@"*dLatitude : %@", latitude);
    NSLog(@"*dLongitude : %@",longitude);

    //load in the times from the json
    NSString *myURLString = [NSString stringWithFormat:@"http://muslimsalat.com/%@,%@/daily/5.json", latitude, longitude];
    NSURL *url = [NSURL URLWithString:myURLString];
    NSData *jsonData = [NSData dataWithContentsOfURL:url];

    if(jsonData != nil)
    {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        NSArray *jsonArray = (NSArray *)result; //convert to an array
        if (error == nil)
            NSLog(@"%@", result);
            NSLog(@"%@", jsonArray);
            for (id element in jsonArray) {
                NSLog(@"Element asr: %@", [element objectForKey:@"asr"]);
        }
    }
}

我如何获得当前时间并确定接下来要进行哪个祷告?

谢谢!

4

3 回答 3

1

获取日期列表,然后使用NSPredicate将该列表过滤为 dates >= [NSDate date],然后按升序对其进行排序。然后过滤后的排序数组中的第一项将是下一个日期。

于 2013-07-04T20:12:08.897 回答
1

您需要使用[result objectForKey:@"items"]. 然后使用 . 将字符串值转换为NSDate's NSDateFormatter。然后,遍历新字典并找到与现在使用的最小时间间隔的时间[currentDate timeIntervalSinceNow]。这是你的下一个祷告。

于 2013-07-04T22:32:59.700 回答
0

阅读 Apple 的日期和时间编程指南。您将找到将字符串时间(“2:15am”等)转换为NSDate对象的方法、获取当前日期和时间的方法(例如[NSDate new]返回当前日期和时间)以及比较日期/时间的方法,以便您找到下一个。

于 2013-07-04T20:18:42.403 回答