Domanda

Sto cercando di utente NSJSONSerialization In un'app Mac, ma non riesco a farlo funzionare, sto cercando di farlo caricare Usertimeline e mostrare il testo dell'ultimo tweet, e questo è tutto, ma non riesco a trovare il modo corretto per usarlo. L'API che ho provato a usare:

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

E

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

Ma nessuno dei due ho avuto un aspetto, è così che ho cercato di usare 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);

Questo è proprio quello che ho usato per vedere se li ha caricati, ma mi ha fallito.

Ho anche provato a provare questo metodo http://www.raywenderlich.com/5492/working-with-json-in-ios-5 Tuttavia, è solo per iOS e non so come lo farei funzionare su Mac.

Eventuali collegamenti su come utilizzare NSJSONSerialization Perché Mac sarebbe geniale, o qualsiasi aiuto potrebbe anche essere un grande ringraziamento. Ho provato a ricevere aiuto sui forum degli sviluppatori Apple, ma senza fortuna lì.

È stato utile?

Soluzione

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
}

Altri suggerimenti

La mia domanda riceve anche il suo ultimo tweet. Dovresti considerare i limiti al server con queste richieste (350 all'ora). Se si supera questo limite, non vi è nsarray in nsdata, è NSDictionary, che descrive l'errore e la richiesta, che verrà senza dubbio preso in considerazione. L'ho fatto in questo modo:

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);        
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top