Question

Je suis en train de l'utilisateur NSJSONSerialization dans le mac app, mais je ne peux pas le faire fonctionner, je suis en train de l'obtenir pour charger UserTimeline, et de montrer le texte de la dernière tweet, et c'est ça, mais je n'arrive pas à trouver la bonne façon de l'utiliser.l'api, j'ai essayé de l'utiliser :

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

Et

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

mais aucun d'eux j'ai eu l'examiner, c'est la façon dont j'ai essayé de l'utiliser 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);

c'est juste ce que j'ai utilisé pour voir si c'est chargé, mais il est en situation d'échec, moi.

J'ai aussi essayé vais essayer cette méthode http://www.raywenderlich.com/5492/working-with-json-in-ios-5 cependant c'est uniquement pour iOS et je ne sais pas comment je voudrais le faire fonctionner sur Mac.

Tous les liens sur la façon d'utiliser NSJSONSerialization pour mac, ce serait génial, ou toute aide pourrait serait également un grand merci.J'ai essayé d'obtenir de l'aide sur les forums des développeurs apple, mais pas de chance là-bas.

Était-ce utile?

La solution

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
}

Autres conseils

Ma demande a aussi reçu son dernier tweet.Vous devriez considérer les limites pour le serveur avec ces demandes (350 par heure).Si vous dépassez cette limite, il n'y a pas de nsarray dans nsdata il est nsdictionary, qui décrit l'erreur et la demande, qui sera sans aucun doute être pris en compte.J'ai fait de cette façon:

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);        
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top