2012-08-15 2 views
0

"date"에 대한 NSLog가 null을 반환 할 때 내 데이터를 사용하려고합니다. 웹 페이지의 내 json은 다음과 같습니다.NSJSON "date"가 null을 반환합니다.

[{ "date": "2012-08-13 12:00:00", "timezone_type": 3, "timezone": "EST5EDT"}, { "date ":"2012-08-13 10:00:00 ","timezone_type ": 3,"timezone ":"EST5EDT "}, {"date ":"2012-08-13 13:00:00 ","timezone_type "3,"시간대 ""하는 .m

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 
//#define kLatestKivaLoansURL [NSURL   URLWithString://@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"] 
#define kLatestKivaLoansURL [NSURL URLWithString:@"http://www.xxxxxxxxxxx.com/appointjson.php"] 

#import "MyAvailTimesViewController.h" 

@interface MyAvailTimesViewController() 

@end 

@implementation MyAvailTimesViewController 
@synthesize json; 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Do any additional setup after loading the view. 
    dispatch_async(kBgQueue, ^{ 
     NSData* data = [NSData dataWithContentsOfURL: 
         kLatestKivaLoansURL]; 
     [self performSelectorOnMainThread:@selector(fetchedData:) 
           withObject:data waitUntilDone:YES]; 

    }); 
} 
- (void)fetchedData:(NSData *)responseData { 
    //parse out the json data 
    NSLog(@"%@",responseData); 
    NSError* error; 
    NSDictionary* json = [NSJSONSerialization 
          JSONObjectWithData:responseData 

          options:kNilOptions 
          error:&error]; 

    NSArray* latestLoans = [json objectForKey:@"date"]; 
    NSLog(@"date: %@", latestLoans); 
    //NSLog(@"output2: %@", json); 

I 드 responseData가

#import <UIKit/UIKit.h> 

@interface MyAvailTimesViewController : UIViewController { 
} 

@property(nonatomic, retain) NSDictionary *json; 
@end 

NS 개의 로그이다 .H :EST5EDT "} 여기서

및 내 코드2012-08-15 15 : 17 : 54.114 GBSB [480 : 15b03] < 3c21444f 43,545,950 45,206,874 6d6c2050 55424c49 4320222d 2f2f5733 432f2f44 48544d4c 20312e30 54,442,058 20,547,261 6e736974 696f6e61 6c2f2f45 4e222022 68,747,470 3a2f2f77 77772e77 332e6f72 672f5452 2f786874 6d6c312f 4454442f 7868746d 6c312d74 72616e73 6974696f 6e616c2e 64,746,422 3e0a3c68 746d6c20 786d6c6e 733d2268 7474703a 772e7733 2e6f7267 2f313939 392f7868 746d6c22 3e0a3c68 6561643e 0a3c7469 746c653e 47,425,342 204a534f 4e204665 2f2f7777 65643c2f 7469746c 653e0a3c 2f686561 643e0a5b 7b226461 7465223a 22,323,031 322d3038 2d313320 31323a30 303a3030 222c2274 696d657a 6f6e655f 74,797,065 223a332c 2274696d 657a6f6e 65223a22 45,535,435 45,445,422 7d2c7b22 64,617,465 223a2232 3031322d 30382d31 33,203,130 3a30303a의 3030222c 2274696d 657a6f6e 655f7479 7065223a 332c2274 696d657a 6f6e6522 3a224553 54354544 54227d2c 7b226461 7465223a 22323031 322d3038 2d313320 31333a3030a3030 222c2274 696d657a 6f6e655f 74797065 223a332c 2274696d 657a6f6e 652 23a22 45535435 45445422 7d5d3c62 6f64793e 0a0a3c2f 626f6479 3e0a3c2f 68746d6c 3e>

해상도를 찾는 데 어려움이 있습니다.

+0

흠 ... 나는'json'이'NSDictionary'라고 생각하지 않습니다. NSArray와 비슷합니다. 'json.count'를 사용하여 사전이나 배열인지 확인할 수 있습니다. – aforaudrey

+0

어떻게 그럴 수 있습니까? 나는 새롭다 – TIDev

+0

아래 토미가 정확히 무엇을 의미하는지 정확히 말하고있다. – aforaudrey

답변

0

이 :

[{"date":"2012-08-13 12:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 10:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 13:00:00","timezone_type":3,"timezone":"EST5EDT"}] 

배열 아니라 사전이다. [NSJSONSerialization JSONObjectWithData:...을 수행 할 때 리턴 된 오브젝트는 NSArray이어야합니다. 따라서 objectForKey:으로 전화를 걸면 예외가 표시됩니다.

nil이 나오면 파싱을 요청한 JSON 데이터의 형식이 잘못되었다는 것이 현명한 결론입니다.

당신이 게시 NSData, 당신의 서버가 실제로 반환 확인 갖는 명확하게 유효한 JSON 아닌

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>GBSB JSON Feed</title> 
</head> 
[{"date":"2012-08-13 12:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 10:00:00","timezone_type":3,"timezone":"EST5EDT"},{"date":"2012-08-13 13:00:00","timezone_type":3,"timezone":"EST5EDT"}]<body> 

</body> 
</html> 

; JSON을 표시하는 HTML 문서입니다.

+0

서버에서 데이터를 추출 중이며 변경 될 것입니다. 나는 json을 하드 코딩하고 싶지 않다. 어떤 제안? 당신은 내가 그냥 PHP는 부분과 아무 제목이나 PHP 파일에 다른 정보가 있어야 말하고 있습니까? – TIDev

+0

올바른 JSON이라면 첫 번째 문자는'[{ "date": "2012-08-13 12 :'이고 마지막 문자는'_type"이됩니다 : 3, "timezone": "EST5EDT"}]'. 그것이 PHP가 생성 한 것과 정확히 같다고 가정합니다. 예, 포장 된 모든 HTML 내용을 덤프합니다. – Tommy

+0

마지막으로! 도와 주셔서 감사합니다. – TIDev

관련 문제