2012-02-26 4 views
2

mac app에 NSJSONSerialization 사용자를 보내려고했지만 작동하지 않습니다. UserTimeline을로드하고 마지막 짹짹 텍스트를 표시하려고합니다. 그렇긴하지만 그것을 사용하는 적절한 방법을 찾지 못하는 것 같습니다. 이 API는 내가 사용하는 것을 시도했다 :MAC 용 NSJSONSerialization 사용

http://api.twitter.com/1/statuses/user_timeline.json?screen_name=AlexTrott_ 

그리고

http://twitter.com/statuses/user_timeline/AlexTrott_.json 

을 있지만 내가보기를 했어 어느 쪽이 내가 NSJSONSerialization 사용하려고했습니다 방법입니다

NSString *tweets = [NSURL URLWithString:@"http://twitter.com/statuses/user_timeline/AlexTrott_.json"]; 
    NSData *jsonData = [tweets dataUsingEncoding:NSUTF8StringEncoding]; 
    NSError *e = nil; 
    NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e]; 
    NSLog(@"%@", json); 

그것이로드 된 것인지 확인하는 데 사용 된 것이지만 실패한 것입니다.

나는 또한이 방법을 시도하려고했다. http://www.raywenderlich.com/5492/working-with-json-in-ios-5 그러나 iOS 용으로 만이 것이고 Mac에서 어떻게 작동하는지 알 수 없다.

mac에 NSJSONSerialization을 사용하는 방법에 대한 링크는 훌륭하거나 훌륭한 도움이 될 수 있습니다. 나는 애플 개발자 포럼에 도움을 얻는 것을 시도했다, 그러나 운이 없다.

답변

2
NSURL *tweets = [NSURL URLWithString:@"http://twitter.com/statuses/user_timeline/AlexTrott_.json"]; 
    //NSData *jsonData = [tweets dataUsingEncoding:NSUTF8StringEncoding]; 
    //NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e]; 
NSData *jsonData=[NSData dataWithContentsOfURL:tweets]; 
NSError *e = nil; 

    id yourJsonData=[NSJSONSerialization JSONObjectWithData:jsonData options: 
       NSJSONReadingMutableContainers error:&error]; 
NSLog("%@",yourJsonData);//you must get a json object here 

//let's assume you need to get the key kalled user_name from your JSON Response 

NSDictionary *theJson=[yourJsonData objectForKey:@"user_name"]; 

for(NSMutableArray *usernames in theJson){ 

// do something with usernames from your object 
} 
+0

, 서버와이 마법처럼 일했다 –

+0

감사 커뮤니케이션을 않는 클래스 : 나는 이런 식으로했다! –

0

내 응용 프로그램도 마지막 트윗을받습니다. 이러한 요청으로 서버 한계를 고려해야합니다 (시간당 350). 이 제한을 초과하면 nsdata에는 nsdata가 없으므로 nsdictionary는 오류와 요청을 설명하며 이는 의심의 여지없이 고려됩니다. 그런데

NSError* error = nil; 

id responseBody = [NSJSONSerialization JSONObjectWithData:data 
                options:NSJSONReadingMutableContainers 
                error:&error]; 

if (error) { 

    [[NSAlert alertWithError:error] runModal]; 
} 

if ([responseBody respondsToSelector:@selector(objectAtIndex:)]) { 

    else { 

     if ([responseBody count]) { 

      NSString* currentStatus = [[responseBody objectAtIndex:0] objectForKey:@"text"]; //now you can do smth with your status)) 
     } 
     else { 

      NSLog(@"You have not tweetted yet!"); 
     } 
    } 
} 
else if([responseBody respondsToSelector:@selector(objectForKey:)]) { 

    NSString* error = [responseBody objectForKey:@"error"]; 
    NSLog(@"CURRENT STATUS ERROR => %@", error);   
}