2012-02-26 3 views
2

私はMACアプリケーションでNSJSONSerializationのユーザーにしようとしていますが、うまく動作しません。私はUserTimelineをロードして、最後のつぶやきのテキストを表示しようとしています。そうですが、私はそれを使う正しい方法を見つけることができないようです。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); 
私が使用しようとしたAPI

これは、それがロードされているかどうかを確認するために使ってきたものですが、失敗しています。

私はまたこの方法を試してみました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);   
} 
関連する問題