2013-07-04 2 views
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"]); 
     } 
    } 
} 

가 어떻게 현재 시간을 얻고 다음 오는기도를 확인할 수 있습니까?

감사합니다.

+0

질문을 게시하는 방법이 맞는지는 모르지만 JSON 형식은 유효하지 않습니다. – danypata

답변

1

[result objectForKey:@"items"]을 사용하여 '항목'사전을 가져와야합니다. 그런 다음 NSDateFormatter을 사용하여 문자열 값을 NSDate으로 변환하십시오. 그런 다음 새 사전을 반복하고 [currentDate timeIntervalSinceNow]을 사용하여 가장 짧은 시간 간격으로 시간을 찾습니다. 이것은 당신의 다음기도입니다.

+0

최근에 일어난 일도주지 못했습니까? 다음 시간이 3 시간이지만 이전 시간이 1 시간 전이라면 이전 시간을 알려주지 않겠습니까? – samiles

+0

이 메소드는 이벤트가 미래인지 과거인지에 따라 양수와 음수를 반환합니다. 가장 작은 양수를 찾아 다음 행사를 알려줍니다. –

+0

아, 물론 고마워요! – samiles

1

날짜 목록을 가져오고 NSPredicate을 사용하여 해당 목록을 날짜 >= [NSDate date]으로 필터링 한 다음 오름차순으로 정렬하십시오. 그런 다음 필터링 된 정렬 된 배열의 첫 번째 항목이 다음 날짜가됩니다.

0

Apple의 Date and Time Programming Guide을 읽으십시오. 문자열 시간 ("2:15 am"등)을 NSDate 객체로 변환하는 메소드, 현재 날짜와 시간을 가져 오는 메소드 (예 : [NSDate new])를 비교하여 날짜/시간을 비교하는 방법을 찾을 수 있습니다. 다음 것을 찾을 수 있습니다.